[英]Deploying a Node.js(+MongoDB) app with Heroku, always Local Port
I've searched and tried a lot of things, and I can't get my deployment to work. 我已经搜索并尝试了很多东西,但无法使我的部署正常工作。 My node.js app works locally, but doesn't work when deployed on heroku.
我的node.js应用程序可在本地运行,但部署在heroku上则无法运行。 The problem is the port I'm using, I believe.
我相信问题是我正在使用的端口。
In my app.js,I have: 在我的app.js中,我有:
1) a server+port: 1)服务器+端口:
var server = require('http').createServer();
var port = process.env.PORT || 5000;
2) The port I want to listen to: (I do think this part is wrong) 2)我想听的端口:(我确实认为这部分是错误的)
server.listen(port, function() {
console.log("Listening on " + port);
});
app.listen(port);
// app.listen(process.env.PORT || 5000);
module.exports = app;
3) And some other stuff 3)和其他一些东西
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//app.use(express.static(__dirname + '/'))
// var routes = require('./routes/index');
var routes = require('./routes/index')(passport);
var users = require('./routes/users');
// make db accessible to the router
app.use(function(req,res,next){
req.db = db;
next();
});
app.use('/', routes);
app.use('/users', users);
// app.use('/api', api);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
//development error handler
//will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
When I'm on my deployed app, and here you can see it says it will redirect to the place I want (at the bottom left) the frozen-fortress.../login/facebook..etc however, when i actually click on it, it gives me localhost:3000/login/facebook...etc 当我使用已部署的应用程序时,在这里您可以看到它说它将重定向到我想要的位置(在左下角) 冷冻堡垒... / login / facebook..etc,但是,当我实际单击时在它上面,它给了我localhost:3000 / login / facebook ... etc
This happens with some of my others links too, but NOT all the links. 我的其他一些链接也会发生这种情况,但并非所有链接都发生这种情况。
What can I do the change the port? 我该如何更改端口? I have tried things like app.listen(port) as well.
我也尝试过app.listen(port)之类的东西。
Thank you in advance! 先感谢您! (Also I apologize if the format of my question looks funky, this is my first time on stackoverflow).
(我也很抱歉,如果我的问题格式看起来很时髦,这是我第一次使用stackoverflow)。
I might be wrong, but 我可能是错的,但是
look at this part of code: 看一下这段代码:
var server = require('http').createServer();
var port = process.env.PORT || 5000;
// ...
server.listen(port, function() {
console.log("Listening on " + port);
});
app.listen(port);
You create two servers here: server
and Express' app
. 您在此处创建两个服务器:
server
和app
。 Both will try to listen on the same port and one of them will fail. 两者都将尝试侦听同一端口,并且其中之一将失败。 I mean, Express already has
http
server inside, you don't need another one: 我的意思是,Express已经内置了
http
服务器,您不需要另一个服务器:
var port = process.env.PORT || 5000;
app = express()
app.listen(port)
app.use(...)
app.get('/', function(...){
// there you go
})
Here's doc about listen()
and other Express things 这是有关
listen()
和其他Express内容的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.