如何使用Express设置iisnode? [英] How to setup iisnode with express?

查看:86
本文介绍了如何使用Express设置iisnode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始研究nodejs.我创建了一个简单的nodejs api(带有express),该API连接到SQL Server数据库并返回结果.开发之后,我遇到了如何托管此节点js API的挑战.我决定将api托管在IIS上.我遇到了不同的错误,最终我使它能够正常工作.感谢互联网上的其他文章. 以下是我遵循的步骤.可能这可以帮助任何新手并尝试在Windows IIS中托管Node.js的人.

I recently started working on nodejs. I created a simple nodejs api (with express) which connect to SQL server database and return result. After my development I had challenge how to host this node js api. I decided to host my api on IIS. I got different errors and in the end I was able to make it work. Thanks to different articles on internet. Below are the steps I followed. May be this can help anyone who is new and trying to host nodejs in windows IIS.

推荐答案

我最近开始研究nodejs.我创建了一个简单的nodejs api(带有express),该API连接到SQL Server数据库并返回结果.开发之后,我遇到了如何托管此节点js API的挑战.我决定将api托管在IIS上.我遇到了不同的错误,最终我使它能够正常工作.感谢互联网上的其他文章. 以下是我遵循的步骤.可能这可以帮助任何新手并尝试在Windows IIS中托管Node.js的人.

I recently started working on nodejs. I created a simple nodejs api (with express) which connect to SQL server database and return result. After my development I had challenge how to host this node js api. I decided to host my api on IIS. I got different errors and in the end I was able to make it work. Thanks to different articles on internet. Below are the steps I followed. May be this can help anyone who is new and trying to host nodejs in windows IIS.

步骤1:安装IISnode.确保根据您的机器选择正确的位版本.我正在使用Windows 10 64位.我安装了iisnode-full-v0.2.21-x64.msi https://github.com/azure/iisnode/wiki/iisnode-releases

Step 1: Install IISnode. Make sure to select correct bit version as per your machine. I was using windows 10 64 bit. I installed iisnode-full-v0.2.21-x64.msi https://github.com/azure/iisnode/wiki/iisnode-releases

第2步:安装URL重写模块 https://www.iis.net/downloads/microsoft/url-rewrite

Step 2: Install URL rewrite module https://www.iis.net/downloads/microsoft/url-rewrite

第3步:供我使用,我在IIS中创建了一个名为节点网站"的新网站.该站点在端口90上运行.将该站点指向可以使用Nodejs api的物理路径.

Step 3: For my use I created a new website in IIS with name "Node Web Site". This site is running on port 90. Point this web site to physical path where your Nodejs api is available.

步骤4:提供节点js api文件夹对"IIS_IUSRS"组的访问权限.如果不提供访问权限,则会出现访问错误.

Step 4: Provide node js api folder access to "IIS_IUSRS" group. You will get access error if don't provide access.

第5步:在您的节点js api文件夹中添加一个web.config文件.在您的配置文件中添加以下代码.这将告诉IIS server.js将由IISnode处理. 注意:我的项目(server.js)中只有一个文件.如果您有多个文件,则可以在此处添加所有这些文件.

Step 5: Add a web.config file in your node js api folder. Add below code in your config file. This will tell IIS that server.js will be handled by IISnode. Note: I have only one file in my project (server.js). If you have multiple files then you can add all those files here.

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

第6步:在配置文件中添加URL重写规则.这是使url用户友好所必需的.否则,您需要在网址中提供.JS文件路径.下面是我在应用程序中拥有的最终配置文件.

Step 6: Add URL rewrite rule in your config file. This is required to make url user friendly. otherwise you need to provide .JS file path in the url. below is the final config file which I have in my application.

<configuration>
  <system.webServer> 

    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>
     <rewrite>
       <rules>
         <rule name="api">
           <match url="api/*" />
           <action type="Rewrite" url="server.js" />
         </rule>
       </rules>
     </rewrite>
	 
	  <security>
       <requestFiltering>
         <hiddenSegments>
           <add segment="node_modules" />
         </hiddenSegments>
       </requestFiltering>
     </security> 
  </system.webServer>
</configuration>

在重写部分之前,我使用url http://localhost/nodesample1/server.js

Before Rewrite section I was calling my application with url http://localhost/nodesample1/server.js

重写网址后可以像

http://localhost/nodesample1/api

第7步:现在您需要在express的get调用中进行更改.您需要在get呼叫中提供完整路径. 例如,在将应用程序托管在IISNode中之前,我的默认get调用代码类似于以下代码段

Step 7: Now you need to make changes in get call of express. you need to provide full path in get call. for example before hosting application in IISNode my default get call code was like below snippet

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser());
app.get('/', function (request, response) {  
    response.write('running');
    response.end();
});

但是在IISNode托管之后,我不得不像下面那样更改我的get呼叫

But after IISNode hosting I had to change my get call like below

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser());
app.get('nodesample1/api', function (request, response) {  
    response.write('running');
    response.end();
});

因为我希望我的网址像" http://localhost/nodesample1/api ",所以我必须提供完整的路径在接听电话.

As I want me url to be like "http://localhost/nodesample1/api" I had to provide complete path in get call.

就是这样.

这种方法对我有用.

这篇关于如何使用Express设置iisnode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆