我正在开发一个网站,我想在localserver / heroku / aws上部署
在localserver和aws上,当调用' nodejs web.js'端口3000工作找到。
当做工头开始时#39;它提供了收听端口3000的反馈,但浏览器没有显示该站点。不在aws上,也不在本地服务器上。
当然,我还没有在heroku上工作。
注意: ' /公共' dir被' / assets'。
取代web.js
var http = require("http"),
// utilities for working with file paths
path = require("path"),
// utilities for accessing the file system
fs = require("fs"),
extensions = {
".html": "text/html",
".css": "text/css",
".js": "application/javascript",
".png": "image/png",
".gif": "image/gif",
".jpg": "image/jpeg",
".ttf": "font/truetype",
".otf": "font/opentype",
".woff": "application/x-font-woff"
};
http.createServer(function(req, res) {
// look for a filename in the URL, default to index.html
var filename = path.basename(req.url) || "index.html",
ext = path.extname(filename),
dir = path.dirname(req.url).substring(1),
// __dirname is a built-in variable containing the path where the code is running
localPath = __dirname + "/assets/";
if (extensions[ext]) {
localPath += (dir ? dir + "/" : "") + filename;
fs.exists(localPath, function(exists) {
if (exists) {
getFile(localPath, extensions[ext], res);
} else {
res.writeHead(404);
res.end();
}
});
}
function getFile(localPath, mimeType, res) {
fs.readFile(localPath, function(err, contents) {
if (!err) {
res.writeHead(200, {
"Content-Type": mimeType,
"Content-Length": contents.length
});
res.end(contents);
} else {
res.writeHead(500);
res.end();
}
});
}
}).listen(process.env.PORT || 3000, function() {
console.log("listening on 3000");
});
Procfile
web: nodejs web.js
的package.json
{
"name": "myapp",
"version": "0.0.1",
"description": "web developer",
"main": "web.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"express": "2.5.x"
},
"engines": {
"node": "0.8.x",
"npm": "1.1.x"
},
"repository": {
"type": "git",
"url": "https://github.com/heroku/node-js-sample"
},
"keywords": [
"node",
"heroku"
]
答案 0 :(得分:0)
默认情况下为:
node web.js
除非你在start {}下的package.json文件中指定,否则。
编辑Procfile
web: node web.js
应该这样做。 :)
答案 1 :(得分:0)
让我们尝试调试它,请提及以下测试结果:
确保节点正在侦听端口3000:
netstat -lnW | grep :3000
成功案例:打印监听器信息
然后尝试连接到端口3000:
telnet localhost 3000
成功案例:打印“已连接到localhost”,输入内容并按Enter键,您应该收到“400 Bad Request”错误页面
在条件部分之前向处理函数添加调试消息,如:
http.createServer(function(req, res) {
console.log(path.basename(req.url) || "no specific page");
...}
成功案例:显而易见