gpt4 book ai didi

node.js - Windows服务器(iis)上的主机 Node js

转载 作者:行者123 更新时间:2023-12-03 09:53:03 24 4
gpt4 key购买 nike

一个月前我开始学习服务器端编码,我使用“express”框架和 mssql 构建了一个带有 get 和 post 请求的 nodejs 项目和 web 服务。
我的项目文件包括一个“main.js”文件和一个“node_modules”文件夹。

我正在尝试在 IIS 上托管这个项目,但不知道如何做,也没有经验。

我是否必须以某种方式打包我的项目。

我可以在 IIS 上托管 nodejs 项目吗?如果是这样,那么我需要执行哪些步骤。
我有一个运行 IIS 的 Windows 服务器,那里安装了 mysql。

最佳答案

这是一步一步...

  • 如果您还没有安装 node , iisnodeurlrewrite
  • 将网站添加到 iis enter image description here
  • 编辑主机文件enter image description here
  • 将您的网站网址添加到主机 enter image description here
  • 检查您的新网站模块以确保安装了 iisnode enter image description here
  • 如果它在那里你很好enter image description here
  • 创建 Node 应用代码JS文件enter image description here
  • 将此代码放入文件

  • var express = require("express");
    var app = express();
    app.get("/", function(req, res) {
    res.send("Hello Worlxxxxd!");
    });
    // This is REQUIRED for IISNODE to work
    app.listen(process.env.PORT, () => {
    console.log("listening");
    });

  • 将web.config文件添加到目录并将此代码放入其中
  • <configuration> 
    <system.webServer>

    <handlers>
    <add name="iisnode" path="node_app.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
    <rules>
    <rule name="nodejs">
    <match url="(.*)" />
    <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Rewrite" url="/node_app.js" />
    </rule>
    </rules>
    </rewrite>

    <security>
    <requestFiltering>
    <hiddenSegments>
    <add segment="node_modules" />
    <add segment="iisnode" />
    </hiddenSegments>
    </requestFiltering>
    </security>
    </system.webServer>
    </configuration>
  • 在浏览器中导航到新站点,您应该会收到此错误,因为您尚未安装 express 包 enter image description here
  • 打开命令提示符并安装 express enter image description here
  • 刷新网页,瞧enter image description here
  • 关于node.js - Windows服务器(iis)上的主机 Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46266609/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com