如何为 Web 应用程序运行 Node.JS 服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16434755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:26:30  来源:igfitidea点击:

How to run Node.JS server for a web application?

node.jsiisnode

提问by Ramanpreet Singh

Info: I am very new to node.JS!

信息:我对 node.JS 很陌生!

I have written a sample server that can listen to http requests on port XXXX. When I run this server from commandline (Windows) it seems to work well. It responds to the requests made to localhost:XXXX when opened in a browser.

我编写了一个示例服务器,可以在端口 XXXX 上侦听 http 请求。当我从命令行 (Windows) 运行此服务器时,它似乎运行良好。在浏览器中打开时,它会响应对 localhost:XXXX 的请求。

Question: Is this how this is supposed to work? For the node server to run, should there always be a CMD prompt open for the server to listen to requests? Can I not do "something" with IISNode?

问题:这是应该如何工作的吗?要让节点服务器运行,是否应该一直打开一个 CMD 提示让服务器监听请求?我不能用 IISNode 做“某事”吗?

I understand that if I make a request to a JS files, which is noted in IISNode as a Node.JS file and that NODE should be handling it; then I will have Node handling the request for me. But then this assumes that IIS is the web server for me and that specific requests can be handled by Node.

我知道如果我向 JS 文件发出请求,在 IISNode 中将其标记为 Node.JS 文件,并且 NODE 应该处理它;然后我会让 Node 为我处理请求。但是,这假设 IIS 是我的 Web 服务器,并且特定请求可以由 Node.js 处理。

I hope I am making sense here! :)

我希望我在这里说得通!:)

采纳答案by Ramanpreet Singh

I solved it using a proper method. Yes, IISNode it is.. But none of comments seemed to answer how to "run" app.js for different applications hosted on same IIS (which is also serving PHP, ASPX, etc)

我用正确的方法解决了它。是的,它是 IISNode ......但似乎没有任何评论回答如何为托管在同一 IIS 上的不同应用程序“运行”app.js(也提供 PHP、ASPX 等)

Step 1. Edit your node application's entry-point (typically) app.js for the new URL structure.

步骤 1. 为新的 URL 结构编辑节点应用程序的入口点(通常)app.js。

An express app assumes that it owns the entire URL space and starts the URLs from the root itself, as shown:

express 应用假定它拥有整个 URL 空间并从根本身开始 URL,如下所示:

Default EXPRESS App.js

默认 EXPRESS App.js

Edit you app.js to look like the following (but put YOUR app's directory name instead of “aaspass”!!):

编辑你的 app.js 看起来像下面这样(但把你的应用程序的目录名而不是“aaspass”!):


app.js modified as per directory structure of app hosted on IIS


Now put a web.config file at the root of your app which looks like the following (You may use this template: webconfig).


app.js 根据托管在 IIS 上的应用程序的目录结构进行修改


现在将 web.config 文件放在您的应用程序的根目录下,如下所示(您可以使用此模板:webconfig)。

Again edit the file and change the name “aaspass” to your app's directory name.


Modified We.Config to add rules to redirect to relevant app.js

再次编辑文件并将名称“aaspass”更改为应用程序的目录名称。


修改 We.Config 添加规则重定向到相关的 app.js

Thats it! You may do this for as many apps as required and host them on SAME server.

就是这样!您可以根据需要为尽可能多的应用程序执行此操作,并将它们托管在相同的服务器上。

回答by Tomasz Janczuk

On Windows you have two options of hosting node.js applications:

在 Windows 上,您有两种托管 node.js 应用程序的选项:

  1. Self-host the node.exe process just like you would on *nix. During development you will probably just start it from the command line. In production you want to come up with a mechanism that will provide process lifetime management around node.exe (e.g. start it when the OS starts). The most reasonable way of doing it on Windows is to use Windows Services (also known as NT Services). A component that can help you do this is http://nssm.cc/.
  2. Host node.js with the IIS using iisnode (http://github.com/tjanczuk/iisnode). Compared to self-hosting this method has a number of benefits outlined in https://github.com/tjanczuk/iisnode/wiki. But you also want to explore the performance implications (not all of them bad actually): http://tomasz.janczuk.org/2012/06/performance-of-hosting-nodejs.html.
  1. 就像在 *nix 上一样自托管 node.exe 进程。在开发过程中,您可能只会从命令行启动它。在生产环境中,您希望提出一种机制来提供围绕 node.exe 的进程生命周期管理(例如,在操作系统启动时启动它)。在 Windows 上执行此操作的最合理方法是使用 Windows 服务(也称为 NT 服务)。可以帮助您执行此操作的组件是http://nssm.cc/
  2. 使用 iisnode ( http://github.com/tjanczuk/iisnode)使用 IIS 托管 node.js。与自托管相比,此方法具有https://github.com/tjanczuk/iisnode/wiki 中概述的许多优点。但您还想探索性能影响(实际上并非所有问题都不好):http: //tomasz.janczuk.org/2012/06/performance-of-hosting-nodejs.html

回答by SharpCoder

What worked for me:

什么对我有用:

  1. Install IISNode
  2. Install URL Rewrite module of IIS
  3. Add web.config file in your Node.js app/folder. Here are the content of web.config file:

    In the handler, I just need to point to app.js (typical entry point of your application). I have not made changed to any of my routes (there is no need to append any text).

  1. 安装 IISNode
  2. 安装 IIS 的 URL Rewrite 模块
  3. 在 Node.js 应用程序/文件夹中添加 web.config 文件。以下是 web.config 文件的内容:

    在处理程序中,我只需要指向 app.js(应用程序的典型入口点)。我没有更改任何路线(无需附加任何文本)。

..

..

<configuration> 
        <appSettings>
            <add key="NODE_ENV" value="production" />
        </appSettings>
          <system.webServer>



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

     <rewrite>
      <rules>
       <clear />
        <rule name="cdw">
          <match url="/*" />
          <action type="Rewrite" url="server/app.js" />
        </rule>
      </rules>
    </rewrite>

  </system.webServer>
</configuration>

回答by Hector Correa

If you are on Windows you can (and probably should) run Node.js under IIS:

如果您使用的是 Windows,您可以(并且可能应该)在 IIS 下运行 Node.js:

http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx

http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx