简体   繁体   English

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

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

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. 我编写了一个可以侦听端口XXXX上的http请求的示例服务器。 When I run this server from commandline (Windows) it seems to work well. 当我从命令行(Windows)运行此服务器时,它似乎运行良好。 It responds to the requests made to localhost:XXXX when opened in a browser. 它响应在浏览器中打开时对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? 要使节点服务器运行,是否应始终打开CMD提示以使服务器侦听请求? Can I not do "something" with IISNode? 我不能用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; 我理解,如果我向一个JS文件发出请求,这个文件在IISNode中被记为Node.JS文件并且NODE应该处理它; then I will have Node handling the request for me. 然后我会让Node处理我的请求。 But then this assumes that IIS is the web server for me and that specific requests can be handled by Node. 但是,这假设IIS是我的Web服务器,并且Node可以处理特定请求。

I hope I am making sense here! 我希望我在这里有意义! :) :)

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. 自行托管node.exe进程就像在* 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 (eg start it when the OS starts). 在生产中,您希望提供一种机制,该机制将围绕node.exe提供进程生命周期管理(例如,在OS启动时启动它)。 The most reasonable way of doing it on Windows is to use Windows Services (also known as NT Services). 在Windows上执行此操作的最合理方法是使用Windows服务(也称为NT服务)。 A component that can help you do this is http://nssm.cc/ . 可以帮助您执行此操作的组件是http://nssm.cc/
  2. Host node.js with the IIS using iisnode ( http://github.com/tjanczuk/iisnode ). 使用iisnode( http://github.com/tjanczuk/iisnode )使用IIS主机node.js。 Compared to self-hosting this method has a number of benefits outlined in https://github.com/tjanczuk/iisnode/wiki . 与自托管相比,此方法具有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 . 但是你也想探索性能影响(实际上并不是所有这些影响都很糟糕): http//tomasz.janczuk.org/2012/06/performance-of-hosting-nodejs.html

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(也提供PHP,ASPX等)托管的不同应用程序“运行”app.js

Step 1. Edit your node application's entry-point (typically) app.js for the new URL structure. 步骤1.编辑节点应用程序的入口点(通常)app.js以获取新的URL结构。

An express app assumes that it owns the entire URL space and starts the URLs from the root itself, as shown: Express应用程序假定它拥有整个URL空间并从根本身启动URL,如下所示:

默认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根据IIS上托管的应用程序的目录结构进行了修改


Now put a web.config file at the root of your app which looks like the following (You may use this template: webconfig). 现在将web.config文件放在应用程序的根目录下,如下所示(您可以使用此模板:webconfig)。

Again edit the file and change the name “aaspass” to your app's directory name. 再次编辑文件并将名称“aaspass”更改为应用程序的目录名称。


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

Thats it! 而已! You may do this for as many apps as required and host them on SAME server. 您可以根据需要为多个应用程序执行此操作,并将它们托管在SAME服务器上。

What worked for me: 什么对我有用:

  1. Install IISNode 安装IISNode
  2. Install URL Rewrite module of IIS 安装IIS的URL重写模块
  3. Add web.config file in your Node.js app/folder. 在Node.js应用程序/文件夹中添加web.config文件。 Here are the content of web.config file: 以下是web.config文件的内容:

    In the handler, I just need to point to app.js (typical entry point of your application). 在处理程序中,我只需要指向app.js(应用程序的典型入口点)。 I have not made changed to any of my routes (there is no need to append any text). 我没有改变我的任何路线(没有必要附加任何文字)。

.. ..

<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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM