[英]Node.js azure web app can't access my routes
我有以下index.js
文件:
const express = require('express');
const app = express();
const router = express.Router();
var cors = require('cors');
var http = require('http').Server(app);
const fileUpload = require('express-fileupload');
app.use(fileUpload());
const uploadRoute = require('./routes/upload');
app.use(cors());
app.use(uploadRoute(router));
app.use(router);
http.listen(80, function () {
console.log('listening on *:80');
});
以及以下 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>
現在我將它添加到我的服務器,使它看起來像這樣:
然后我嘗試使用永遠forever start index.js
來啟動服務器,然后我去我的網站並嘗試一條路線,但它只是給了我一個錯誤代碼 500。
誰能告訴我我做錯了什么?
根據我的經驗,您可以像下面那樣修改index.js
試:
const express = require('express');
const app = express();
const router = express.Router();
var cors = require('cors');
var http = require('http').Server(app);
const fileUpload = require('express-fileupload');
app.use(fileUpload());
const uploadRoute = require('./routes/upload');
app.use(cors());
app.use(uploadRoute(router));
app.use(router);
app.set('port', process.env.PORT || 5000);
app.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
此外,對於 500 錯誤,您需要啟用stdout
和stderr
日志記錄以進行故障排除並查看日志內容。 要啟用調試,請按照下列步驟操作:
如果不存在,則在您的根文件夾 ( D:\\home\\site\\wwwroot
) 中創建文件iisnode.yml
。
向其中添加以下幾行。
loggingEnabled: true logDirectory: iisnode
完成后,您可以在D:\\home\\site\\wwwroot\\iisnode
找到日志。
有關更多信息,請參閱文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.