'nodejs web.js'的作品,'工头开始'没有

我正在开发一个网站,我想部署在localserver / heroku / aws上

在localserver和aws上,当调用'nodejs web.js'port 3000 works find。

在做“工头启动”的时候给出端口3000的反馈,但是浏览器不显示这个站点。 不是在aws上,而不是在本地服务器上。

当然,我还没有在heroku上工作。

注意:'/ public'dir被replace为'/ 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" ] 

默认情况下是:

 node web.js 

除非您在开始{}下的package.json文件中指定,否则。

编辑Procfile

 web: node web.js 

这应该做到这一点。 🙂

让我们尝试debugging它,请提及以下testing结果:

  1. 确保节点正在侦听端口3000:

     netstat -lnW | grep :3000 

    成功案例:打印侦听器信息

  2. 然后尝试连接到端口3000:

     telnet localhost 3000 

    成功案例:打印“连接到本地主机”,键入内容并点击回车,就会出现“400错误请求”错误页面

  3. 在条件部分之前向处理函数添加debugging消息,如下所示:

     http.createServer(function(req, res) { console.log(path.basename(req.url) || "no specific page"); ...} 

    成功案例:显而易见