繁体   English   中英

index.js文件中的Azure Node.js内部服务器错误

[英]Azure Node.js Internal Server Error from index.js file

我有一个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"
}
}

再次感谢你。

看来您没有在问题中张贴完整的代码片段,因此在访问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并将其拖动到浏览器。

然后,节点应用程序将按预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM