[英]Azure Node.js Internal Server Error from index.js file
I have a Node.js app working locally, however I have spent quite a while trying to figure out how to deploy it on Azure through countless tutorials and official guides. 我有一个Node.js应用程序在本地工作,但是我花了很长时间试图通过无数的教程和官方指南找出如何在Azure上部署它。 However, I keep on receiving the following error "The page cannot be displayed because an internal server error has occurred.", which I believe is caused by my index.js file.
但是,我继续收到以下错误“该页面无法显示,因为发生了内部服务器错误。”,我认为这是由我的index.js文件引起的。 My app only consists of an index.js, packagage.json, and a main.html file.
我的应用仅包含index.js,packagage.json和main.html文件。
Could you take a look at my index.js and package.json file below to see if you could spot any errors? 您能否看一下下面的我的index.js和package.json文件,看看是否可以发现任何错误? I appreciate your help.
我感谢您的帮助。
index.js 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 的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"
}
}
Thank you again. 再次感谢你。
Looks like you don't post complete code snippet in the question, so I use a template for displaying main.html when visit the Azure web root url. 看来您没有在问题中张贴完整的代码片段,因此在访问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);
To make this node app work on Azure(IIS), we need a web.config file. 要使此节点应用程序在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>
So folder structure should be like this. 所以文件夹结构应该是这样的。
You don't mention how you deploy the code, I just use Zip Deploy to test. 您没有提到如何部署代码,我只是使用Zip Deploy进行测试。
Create a zip file with all content(including node_modules), go to https://yourwebappname.scm.azurewebsites.net/ZipDeploy
and drag it to browser. 创建一个包含所有内容(包括node_modules)的zip文件,转到
https://yourwebappname.scm.azurewebsites.net/ZipDeploy
并将其拖动到浏览器。
Then the node app should work as expected. 然后,节点应用程序将按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.