[英]Deploy node.js app on heroku succeeds but doesn't work
我有一個非常簡單、直接的 node.js 應用程序 [1] 我想在 heroku 上部署。 雖然我能夠部署它,但無法在瀏覽器中訪問該頁面。
我遵循了“Heroku 上的 Node.js 入門”指南 [2] 中的建議。 當我在本地使用node index.js
運行應用程序時,我可以通過http://localhost:8080/index.jade
訪問該應用程序,但是,當我嘗試在http://immonow.herokuapp.com:8080/index.jade
heroku 上訪問它時http://immonow.herokuapp.com:8080/index.jade
它會給我一個ERR_CONNECTION_REFUSED
HTTP 錯誤代碼。
我如何部署我的應用程序:
git commit -am "made changes"
// 提交更改git push origin master
// 推送到 githeroku create
// 創建 heroku 應用程序git push heroku master
// 推送到herokuheroku ps:scale web=1
// 開始工作我的 node.js 服務器:
#!/usr/bin/env node
var http = require('http')
, jade = require('jade')
, static = require('node-static')
, jadeRe = /\.jade$/
, path = process.argv.slice(2)[0]
, fileServer = new static.Server(path || '.')
http.createServer(function (req, res) {
if (req.url.match(jadeRe)) {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(jade.renderFile('.' + req.url, {
filename: '.' + req.url.replace(jadeRe, '')
}))
} else {
req.addListener('end', function () {
fileServer.serve(req, res)
}).resume()
}
}).listen(8080)
任何幫助,將不勝感激。
[1] https://github.com/takahser/immonow
[2] https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction
由於我無法使用 http 包使其工作,因此我決定改用 express。 至於端口,我不得不這樣做
var port = process.env.PORT || 3000;
app.listen(port);
為了讓它工作[1]。
這是我的完整工作服務器:
/**
* Module dependencies.
*/
var express = require('express');
// Path to our public directory
var pub = __dirname + '/public';
// setup middleware
var app = express();
app.use(express.static(pub));
app.use("/css", express.static(__dirname + '/css'));
app.use("/font", express.static(__dirname + '/font'));
app.use("/img", express.static(__dirname + '/img'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/video", express.static(__dirname + '/video'));
// Set our default template engine to "jade"
// which prevents the need for extensions
// (although you can still mix and match)
app.set('view engine', 'jade');
app.get('/', function(req, res){
res.render('index');
});
app.get('/*', function(req, res){
console.log(req.url.replace("/",""));
res.render(req.url.replace("/",""));
});
// change this to a better error handler in your code
// sending stacktrace to users in production is not good
app.use(function(err, req, res, next) {
res.send(err.stack);
});
/* istanbul ignore next */
if (!module.parent) {
var port = process.env.PORT || 3000;
app.listen(port);
console.log('Express started on port 3000');
}
[1] 參見: Heroku cedar 堆棧上的 Node.js 端口問題
我不得不做的事情..
在根目錄中創建一個 Procfile(一個字面稱為 Procfile 的文件,沒有擴展名)。
在 Procfile 中,鍵入:
web: node server.js
將腳本和引擎添加到 package.json
"scripts": { "start": "node server.js" }
"engines": { "node": "8.9.3" }
在 server.js 中更新端口
var port = process.env.PORT || 8080;
還有為什么..
明確聲明應該執行什么命令來啟動您的應用程序。 Heroku 尋找一個 Procfile 來指定你的進程類型
對於腳本,如果在構建過程中根目錄中不存在 Procfile,則您的 web 進程將通過運行 npm start 來啟動。 對於引擎,指定一個與您正在開發的運行時相匹配並希望在 Heroku 上使用的 Node 版本。
Heroku 已經為您的應用程序分配了一個端口並將其添加到環境中,因此您無法將端口設置為固定數字。
資源:
https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction
https://devcenter.heroku.com/articles/troubleshooting-node-deploys
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.