gpt4 book ai didi

node.js - “nodejs web.js”有效, 'foreman start' 无效

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:18 25 4
gpt4 key购买 nike

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

在本地服务器和 aws 上,调用“nodejs web.js”端口 3000 时可以查找。

当执行“foreman start”时,它给出监听端口3000的反馈,但浏览器不显示该站点。不在 AWS 上,也不在本地服务器上。

当然,我在 Heroku 上还没有任何进展。

注意:“/public”目录已替换为“/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");
});

Proc文件

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 文件中的 start{} 下指定,否则。

编辑Proc文件

web: node web.js

应该可以了。 :)

关于node.js - “nodejs web.js”有效, 'foreman start' 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25077297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com