我有一个非常简单,直接的node.js app [1]我想在heroku上部署.虽然我可以部署它,但无法在浏览器中访问该页面. 我遵循了“Heroku上的Node.js入门”指南[2]中的建议.当我在本地使用节点index.js运
我遵循了“Heroku上的Node.js入门”指南[2]中的建议.当我在本地使用节点index.js运行应用程序时,我能够访问http:// localhost:8080 / index.jade上的应用程序,但是,当我尝试在http:// immonow上的heroku上访问它时. herokuapp.com:8080/index.jade它会给我一个ERR_CONNECTION_REFUSED HTTP错误代码.
我如何部署我的应用:
> git commit -am“进行了更改”//提交更改
> git push origin master //推送到git
> heroku创建//创建heroku应用程序
> git push heroku master //推送到heroku
> heroku 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]见:Node.js port issue on Heroku cedar stack