Deploying a Node.js(+MongoDB) app with Heroku, always Local Port - node.js

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. The problem is the port I'm using, I believe.
In my app.js,I have:
1) a server+port:
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)
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
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
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.
Thank you in advance! (Also I apologize if the format of my question looks funky, this is my first time on 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. 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:
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

Related

NodeJS express auto created app binding to server

I created an express app in NodeJS using express --view=pug expressapp. It created an app.js file
app.js
var createError = require('http-errors');
...
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
//socket setup
var socket = require('socket.io');
var io = socket(app);
io.on('connection', function(socket) {
console.log('made socket connection')
})
module.exports = app;
at the end I try to bind socket.io to app but it expects to bind to server like so
var server = app.listen(3000, function() {
});
//socket setup
var socket = require('socket.io');
var io = socket(server);
I also use nodemon so I run the app using nodemon expressapp and it runs on port 3000. However I can't find where the auto generated express files have setup the server. Because If i try like this
//socket setup
var socket = require('socket.io');
var server = app.listen(3000, function() {
});
var io = socket(server);
I get an error that port 3000 is already in use (by itself) I changed it to 4000 and it works.
Not ideal, if you know how to connect with port 3000 will appreciate the assist.
You shall find it under the directory bin/www

Change default file extension for vash from .vash to .html for vs2015 syntax highlighting

How do you change the default file extension for Vash? I would like syntax highlighting in Visual Studio 2015, so I want to change the extension from .vash to .html.
I read that I should be able to do something like:
app.engine("html", require("vash").__express);
But I can't figure out the syntax, or I have the commands in the wrong order.
I have the code working with .vash files, but if I try to change to .html, I get the following error. I have confirmed that there does exist an index.html file in that folder.
Failed to lookup view "index" in views directory IQuestions.FrontEnd\views"
var http = require("http");
var express = require("express");
var path = require("path");
var logger = require("morgan");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var routes = require("./routes/index");
var users = require("./routes/users");
var app = express();
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.engine("html", require("vash").__express);
app.set("view engine", "vash");
app.set("port", process.env.PORT || 3000);
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require("stylus").middleware(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "public")));
app.use("/", routes);
app.use("/users", users);
// 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: {}
});
});
module.exports = app;
http.createServer(app).listen(app.get("port"), function () {
console.log("Express server listening on port " + app.get("port"));
});
Try this
app.set('view engine', 'html');
app.engine("html", require('vash').__express);

NodeJS Express 4.9 and Socket.IO - 404 error on socket.io.js

I am trying to create an Express app with Socket.IO. It seems like Express has changed quite a lot and most of the code online about getting the 2 to work together is out of date. This is what I have:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io').listen(http);
io.on('connection', function(socket){
console.log('a user connected');
});
// 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(__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(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// 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: {}
});
});
module.exports = app;
The server starts ok and says
info - socket.io started
However the front-end has a 404 error on http://example.com:3000/socket.io/socket.io.js
edit
If I add
http.listen(8080);
The server runs twice on port 3000 and 8080, the 3000 version does not load socket.io.js and the 8080 version does. How can I have it so the server is only running on 3000? changing it to 3000 errors and it tries to listen twice
The answer was to make the changes in bin/www.js instead of app.js
#!/usr/bin/env node
var debug = require('debug')('test1');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var http = require('http').Server(app);
var io = require('socket.io')(http);
var server = http.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
Change the io include to:
var io = require('socket.io')(http);
And towards the bottom add something like:
http.listen(3000, function() {
console.log('Listening on port %d', http.address().port);
});

GET /easyrtc/easyrtc.js 404 on express js 4

i have a 404 problem with EasyRTC on Express.js 4.
The console said : GET /easyrtc/easyrtc.js 404 4.500 ms - 1077
and here's my app.js code :
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var io = require('socket.io');
var easyrtc = require('easyrtc');
var routes = require('./routes/index');
var users = require('./routes/users');
var staticMiddleware = express.static(__dirname + "/public");
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(__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(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
app.get('/easyrtc/*', function (req,res) {
res.sendFile(__dirname + '/static');
})
// 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: {}
});
});
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});
var webSockets = io.listen(server);
var rtc = easyrtc.listen(app, webSockets);
please help me, i've been searching for the solution all of this time and i couldnt find the solution at all. thanks before
NB : i'm generate express.js project from Terminal in linux , using command : express -e
Well, you may just to remove the code below:
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

Nodejs+Expressjs+SocketIO on Heroku gives GET /socket.io/?EIO... 404

I am having problem with setting socketIO 1.1 up to work on Heroku. I'm running Express v4.2. The socketio runs perfectly on localhost. However, on Heroku, it gives this error:
From Heroku log:
2014-09-15T09:40:14.578660+00:00 app[web.1]: GET /socket.io/?EIO=3&transport=polling&t=1410774014583-86 404 6ms - 744b
From browser:
GET http://[mydomainname]/socket.io/?EIO=3&transport=polling&t=1410774080589-97 404 (Not Found) socket.io.js:2680Request.create socket.io.js:2680Request socket.io.js:2614XHR.request socket.io.js:2555XHR.doPoll socket.io.js:2585Polling.poll socket.io.js:2951Polling.doOpen socket.io.js:2895Transport.open socket.io.js:2106Socket.open socket.io.js:1580Socket socket.io.js:1467Socket socket.io.js:1419Manager.open.Manager.connect socket.io.js:272(anonymous function)
I have followed the recommended configurations in Socket IO documentations. Relevant code as follows:
var express = require('express');
/* routes */
var routes = require('./routes/index');
var users = require('./routes/users');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cons = require('consolidate');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('dust', cons.dust);
app.set('view engine', 'dust');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser('hashionhashion'));
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
//routing
app.use('/', routes);
app.use('/users', users);
/// 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: {}
});
});
//start the server
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
var ioSockets = {};
io.on('connection', function(socket) {
ioSockets[socket.id] = socket;
socket.emit('welcome', {message: socket.id});
socket.on('disconnect', function(socket) {
delete ioSockets[socket.id];
});
});
exports.ioSockets = ioSockets;
module.exports = app;
server.listen(3000, function() {
console.log('Congrats, nothing broke!! Listening on port %d', server.address().port);
});
In my client file, the script are as follows:
<script src="my_path_to/socket.io.js"></script>
<script>
var socket = io.connect();
var socketId = '';
socket.on('welcome', function (data) {
socketId = data.message;
});
</script>
I also tried io.connect to "http://localhost", "http://localhost:3000", and my Heroku app domain. All of them produces the same error.
Any help will be greatly appreciated!
The issue ends up to be a express one, which has since been corrected in versions after 4.10.0.
For your own reading:
https://github.com/strongloop/express/issues/2406
https://github.com/jshttp/on-finished/issues/10
Look like you change socket.io path. Because I saw src="my_path_to/socket.io.js". socket.io try to connect but got 404.
Is there a route of express handled '/socket.io/'?
Even you resolve the 404 error. You will hit the other error. Because currently on heroku you must set socket.io connect to a https address. Otherwise socket.io will work under polling.
More detail:
https://github.com/Automattic/engine.io/issues/261

Resources