我有我的第一个node.js应用程序(在本地运行良好) - 但我无法通过heroku部署它(也是第一次使用heroku)。代码如下。 SO 不允许我编写这么多代码,所以我只想说在本地运行代码以及在我的网络中都没有问题。
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (request, response) {
console.log('request starting for ');
console.log(request);
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
console.log(filePath);
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
}
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
else {
response.writeHead(404);
response.end();
}
});
}).listen(5000);
console.log('Server running at http://127.0.0.1:5000/');
任何想法 ?
Heroku 动态为您的应用程序分配端口,因此您无法将端口设置为固定数字。 Heroku 将端口添加到环境中,因此您可以从那里拉取它。将您的收听切换到此:
.listen(process.env.PORT || 5000)
这样,当您在本地测试时,它仍然会侦听端口 5000,但它也可以在 Heroku 上运行。重要的提示 - PORT单词必须大写。
您可以查看 Node.js 上的 Heroku 文档here.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)