我有一个Node.js应用程序在本地工作,但是我花了很多时间试图通过无数的教程和官方指南来弄清楚如何在Azure上部署它。但是,我继续收到以下错误 “由于发生内部服务器错误,因此无法显示该页面。”,我认为这是由我的index.js文件引起的。我的应用程序仅包含index.js,packagage.json和main.html文件。
您能看看我下面的index.js和package.json文件吗 如果可以发现任何错误?感谢您的帮助。
index.js
var express = require('express');
var app = express();
app.render('main', function(err, html) {
console.log(html)
});
var port = process.env.PORT || 1337;
server.listen(port);
console.log("Server running at http://localhost:%d", port);
package.json
{
"name": "myLocalProj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^2.6.1",
"express": "^4.16.3",
"path": "^0.12.7"
}
}
再次感谢您。
答案 0 :(得分:0)
您似乎没有在问题中发布完整的代码段,因此在访问Azure Web根URL时,我使用了一个模板来显示main.html。
var express = require('express');
var app = express();
app.set('views', __dirname);
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
// This method only prints main.html in console
app.render('main', function(err, html) {
console.log(html);
});
app.get('/', function (req, res) {
res.render('main');
});
var port = process.env.PORT || 1337;
app.listen(port);
console.log("Server running at http://localhost:%d", port);
要使此节点应用程序在Azure(IIS)上运行,我们需要一个web.config文件。
<?xml version="1.0" encoding="utf-8"?>
<!--
This configuration file is required if iisnode is used to run node processes behind
IIS or IIS Express. For more information, visit:
https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->
<configuration>
<system.webServer>
<!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
<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" />
<!--
You can control how Node is hosted within IIS using the following options:
* watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
* node_env: will be propagated to node as NODE_ENV environment variable
* debuggingEnabled - controls whether the built-in debugger is enabled
See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
-->
<!--<iisnode watchedFiles="web.config;*.js"/>-->
</system.webServer>
</configuration>
所以文件夹结构应该是这样的。
您没有提到如何部署代码,我只是使用Zip Deploy进行测试。
创建一个包含所有内容(包括node_modules)的zip文件,转到https://yourwebappname.scm.azurewebsites.net/ZipDeploy
并将其拖动到浏览器。
然后,节点应用程序应该可以按预期运行。