Ron*_*nin 6 git heroku node.js
我有一个非常简单、直接的 node.js 应用程序 [1] 我想在 heroku 上部署。虽然我能够部署它,但无法在浏览器中访问该页面。
我遵循了“Heroku 上的 Node.js 入门”指南 [2] 中的建议。当我使用node index.js本地运行应用程序时,我可以在 上访问该应用程序http://localhost:8080/index.jade,但是,当我尝试在 heroku 上访问它时,http://immonow.herokuapp.com:8080/index.jade它会抛出一个ERR_CONNECTION_REFUSEDHTTP 错误代码。
我如何部署我的应用程序:
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)
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激。
[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);
Run Code Online (Sandbox Code Playgroud)
以便让它发挥作用[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');
}
Run Code Online (Sandbox Code Playgroud)
[1] 请参阅:Heroku cedar 堆栈上的 Node.js 端口问题
| 归档时间: |
|
| 查看次数: |
6951 次 |
| 最近记录: |