我正在澳大利亚东南地区的 Azure 共享资源上运行非常简单的 Express 网站。网站在端口 3000 上本地运行,但是部署到 Azure 时根本不会呈现。当尝试从 Azure 运行时抛出以下错误
node server.js
events.js:141
throw er; // Unhandled 'error' event
Error: listen EADDRINUSE 0.0.0.0:3000
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at Server._listen2 (net.js:1234:14)
at listen (net.js:1270:10)
at Server.listen (net.js:1366:5)
at EventEmitter.listen (D:\home\site\wwwroot\node_modules\express\lib\application.js:617:24)
at Object.<anonymous> (D:\home\site\wwwroot\server.js:21:5)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
Server.js 看起来像
var express = require('express');
var app = express();
var path = require("path");
var port = process.env.PORT || 3000;
console.log(__dirname);
app.use(express.static('public'));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname+'/index.html'));
});
app.get('/contact', function (req, res) {
res.sendFile(path.join(__dirname+'/contact.html'));
});
app.listen(port, function () {
console.log('Example app listening on port!' + port);
});
不确定我错过了什么?
谢谢
最佳答案
Azure 网站允许通过名为 iisnode 的 IIS 模块托管 Node.js 应用程序。请参阅wiki让iisnode知道它。
特别注意以下内容。
Minimal changes to node.js application code. The iisnode module enables hosting of existing HTTP node.js applications with very minimal changes. Typically all that is required is to change the listed address of the HTTP server to one provided by the iisnode module via the process.env.PORT environment variable.
因此,您不需要在 Kudu 控制台中通过命令 node server.js
手动启动 Node 应用程序。请尝试仅通过FTP或VS部署或更新代码,并直接访问网站检查部署是否成功。如果应用程序不起作用,您可以尝试在 Azure 门户上重新启动该应用程序。
如果你想查看正在运行的进程,可以在Kudu工具的Process explorer
中查看,如下图。
如有任何疑问,请随时告诉我。
关于node.js - Azure 上托管的 Express 网站出现意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36880755/