我已在Azure中部署了一个节点应用程序,并希望对其进行配置。这是我要测试的文件“ index.js”的内容:
const express = require('express');
const app = express();
const bodyparser = require('body-parser');
const cors = require('cors');
var cats = [{name:'Lily'}, {name: 'Lucy'}];
var corOptions = {
origin: '*',
optionsSuccessStatus: 200
};
app.use(cors(corOptions));
app.listen(3000,() =>{
console.log('Server started :)');
});
app.route('/api/cats').get((req, res) =>{
console.log(req.hostname);
console.log(req);
res.send(cats);
});
console.log("Server is running...");
这是package.json文件
{
"name": "app-service-hello-world",
"description": "Simple Hello World Node.js sample for Azure App Service",
"version": "0.0.1",
"private": true,
"license": "MIT",
"author": "Microsoft",
"engines": {
"node": ">=6.9.1"
},
"scripts": {
"start": "node index.js"
},
"dependencies": {
"cors": "^2.8.4",
"express": "^4.16.3"
}
}
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
<add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<!-- Do not interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^index.js\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<!-- All other URLs are mapped to the node.js site entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="index.js"/>
</rule>
</rules>
</rewrite>
<!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>
<!-- Make sure error responses are left untouched -->
<httpErrors existingResponse="PassThrough" />
<!--<iisnode watchedFiles="web.config;*.js"/>-->
</system.webServer>
</configuration>
我正在使用下面的以下URL测试应用程序,但未获得预期的输出:https://firstnodeapp1.azurewebsites.net/api/cats
我在本地以http://localhost:8000/api/cats进行了测试,并且工作正常。因此,实际上要担心缺少什么。
我非常感谢您提供任何正确配置它的帮助。
答案 0 :(得分:0)
将端口更改为process.env.port
,然后重新部署您的代码即可。
说明:
Azure Web App是sand box的模型,可以通过仅两个已经公开的80(HTTP)和443(HTTPS)TCP端口进行访问。
使用process.env.port
,Azure将为Nodejs服务器创建一个命名管道以供侦听,并将请求从443端口传递到命名管道。您应用内的任何特定端口均无效,并会导致服务器内部错误(500)。