如何在 azure 网站子目录中运行节点.js应用

How to run a node.js app in an azure website subdirectory

本文关键字:运行 节点 js 应用 子目录 azure 网站      更新时间:2023-09-26

我已经使用 Azure 创建了一个网站,我想在子目录(例如 app1/test)中运行一个 node.js 应用程序,而不是创建一个新网站并在根目录中运行该应用程序。

这是我的目录结构

-wwwroot/web.config

-wwwroot/index.html

-wwwroot/app1/test/package.json

-wwwroot/app1/test/server.js

所以有一个索引.html在根目录中,然后我在测试子目录中有服务器.js。

这是我的服务器.js:

var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World'n');
}).listen(port);

这是我的包.json:

{
  "name": "test",
  "version": "0.0.0",
  "description": "test",
  "main": "server.js",
  "author": {
    "name": "name",
    "email": ""
  },
  "dependencies": {
  }
}

我的网络配置:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <location path="app1/test">
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="false" />
    <!-- indicates that the server.js file is a node.js application 
    to be handled by the iisnode module -->
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
            <match url="iisnode.+" negate="true" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="Rewrite" url="server.js" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>  
  </location>
    <system.webServer>
     <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>
    </system.webServer>
  </configuration>

但我得到

"无法显示页面,因为发生了内部服务器错误。"

当我点击域名/App1/测试

不确定我的 web.config 出了什么问题,以及我是否需要手动安装 iisnode。

我感谢任何帮助。

谢谢

我解决了!以下作品!

<match url="app1/test" />
<action type="Rewrite" url="app1/test/server.js" />

您需要检查您的请求 URL 是否位于您正在检查的位置,在本例中:

var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
  if(req.url === '/app1/test')
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World'n');
}).listen(port);

然后你可以通过转到页面来测试它

http://localhost:1337/app1/test