'nodejs web.js'的作品,'工头开始'没有 [英] 'nodejs web.js' works, 'foreman start' doesn't
问题描述
我正在开发一个网站,我想在localserver上部署/ heroku / aws
在localserver和aws上调用'nodejs web.js'port 3000找到工作。
在做'领班开始'时,它会听到端口3000的反馈,但浏览器不显示该网站。
当然,我还没有在heroku上工作过。
注意:
'/ public'dir已被替换为'/ assets'。
$ b
web.js
var http = require(http),
//用于处理文件路径的实用程序
path = require(路径),
//访问文件系统的工具
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){
//在URL中查找文件名,默认为index.html
var filename = path.basename(req.url)||index.html,
ext = path.extname(filename),
dir = path.dirname(req.url).substring(1 ),
// __dirname是包含代码运行路径的内置变量
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();
}
}) ;
}
函数getFile(localPath,mimeType,res){
fs.readFile(localPath,function(err,contents){
if(!err) {
res.writeHead(200,{
Content-Type:mimeType,
Conten t-Length:contents.length
});
res.end(contents);
} else {
res.writeHead(500);
res.end();
}
});
$ b})。listen(process.env.PORT || 3000,function(){
console.log(listen on 3000);
});
Procfile
web:nodejs web.js
包.json
{
name:myapp,
version :0.0.1,
description:web developer,
main:web.js,
scripts:{
test :echo \错误:未指定测试\&& 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
]
我们试着调试它,请提及以下测试结果:
-
然后尝试连接到端口3000:
telnet localhost 3000
成功案例:打印连接到本地主机,键入内容并点击回车,就会出现400错误请求错误page
-
在条件部分之前向处理函数添加调试消息,如下所示:
http.createServer(function(req,res){
console.log(path.basename(req.url)||no specific page);
...}
成功案例:显而易见
li>
确保节点正在监听端口3000:
netstat -lnW | grep:3000
成功案例:打印侦听器信息
I am developing a website, which I want to deploy on localserver/heroku/aws
On localserver and aws, when calling 'nodejs web.js' port 3000 works find.
When doing 'foreman start' it gives the feedback of listening to port 3000, but the browser doesn't show the site. Not on aws, and not on a localserver.
Of course, I got nothing working on heroku yet.
Note: '/public' dir was replaced with '/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"
]
Lets try to debug it, please mention the following test results:
make sure node is listening to port 3000:
netstat -lnW | grep :3000
success case: prints the listener information
then try to connect to port 3000:
telnet localhost 3000
success case: prints "Connected to localhost", type something and hit the enter, you should get a "400 Bad Request" error page
add a debug message to the handler function before the conditional part, something like:
http.createServer(function(req, res) { console.log(path.basename(req.url) || "no specific page"); ...}
success case: obvious
这篇关于'nodejs web.js'的作品,'工头开始'没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!