Aleksandar Aleksandar - 5 months ago 14
Node.js Question

Unhandled 'error' event?

when I add all three files (dishRouter.js, promoRouter.js and leaderRouter.js in app.js) shows me the following error

error image

This is my app.js

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 dishRouter = require('./routes/dishRouter');
var promoRouter = require('./routes/promoRouter');
var leaderRouter = require('./routes/leaderRouter');

var app = express();

// 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(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/dishes',dishRouter);
app.use('/promotions',promoRouter);
app.use('/leadership',leaderRouter);

// 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;


This is dishRouter.js

var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');

var hostname = 'localhost';
var port = 3000;

var app = express();

app.use(morgan('dev'));

var dishRouter = express.Router();

dishRouter.use(bodyParser.json());

dishRouter.route('/')
.all(function(req,res,next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
next();
})

.get(function(req,res,next){
res.end('Will send all the dishes to you!');
})

.post(function(req, res, next){
res.end('Will add the dish: ' + req.body.name + ' with details: ' + req.body.description);
})

.delete(function(req, res, next){
res.end('Deleting all dishes');
});

dishRouter.route('/:dishId')
.all(function(req,res,next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
next();
})

.get(function(req,res,next){
res.end('Will send details of the dish: ' + req.params.dishId +' to you!');
})

.put(function(req, res, next){
res.write('Updating the dish: ' + req.params.dishId + '\n');
res.end('Will update the dish: ' + req.body.name +
' with details: ' + req.body.description);
})

.delete(function(req, res, next){
res.end('Deleting dish: ' + req.params.dishId);
});

app.use('/dishes',dishRouter);

app.use(express.static(__dirname + '/public'));

app.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});

module.exports = dishRouter;


this is leaderRouter.js

var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');

var hostname = 'localhost';
var port = 3000;

var app = express();

app.use(morgan('dev'));

var leaderRouter = express.Router();

leaderRouter.use(bodyParser.json());

leaderRouter.route('/')
.all(function(req,res,next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
next();
})

.get(function(req,res,next){
res.end('Will send all the leaders to you!');
})

.post(function(req, res, next){
res.end('Will add the leader: ' + req.body.name + ' with details: ' + req.body.description);
})

.delete(function(req, res, next){
res.end('Deleting all leaders');
});

leaderRouter.route('/:leaderId')
.all(function(req,res,next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
next();
})

.get(function(req,res,next){
res.end('Will send details of the leader: ' + req.params.leaderId +' to you!');
})

.put(function(req, res, next){
res.write('Updating the leader: ' + req.params.leaderId + '\n');
res.end('Will update the leader: ' + req.body.name +
' with details: ' + req.body.description);
})

.delete(function(req, res, next){
res.end('Deleting leader: ' + req.params.leaderId);
});

app.use('/leaders',leaderRouter);

app.use(express.static(__dirname + '/public'));

app.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});

module.exports = leaderRouter;


and this is promoRouter.js

var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');

var hostname = 'localhost';
var port = 3000;

var app = express();

app.use(morgan('dev'));

var promoRouter = express.Router();

promoRouter.use(bodyParser.json());

promoRouter.route('/')
.all(function(req,res,next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
next();
})

.get(function(req,res,next){
res.end('Will send all the promotions to you!');
})

.post(function(req, res, next){
res.end('Will add the promotion: ' + req.body.name + ' with details: ' + req.body.description);
})

.delete(function(req, res, next){
res.end('Deleting all promotions');
});

promoRouter.route('/:promotionId')
.all(function(req,res,next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
next();
})

.get(function(req,res,next){
res.end('Will send details of the promotion: ' + req.params.promotionId +' to you!');
})

.put(function(req, res, next){
res.write('Updating the promotion: ' + req.params.promotionId + '\n');
res.end('Will update the promotion: ' + req.body.name +
' with details: ' + req.body.description);
})

.delete(function(req, res, next){
res.end('Deleting promotion: ' + req.params.promotionId);
});

app.use('/promotions',promoRouter);

app.use(express.static(__dirname + '/public'));

app.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});

module.exports = promoRouter;


but to add only one router, works perfectly, why?

I use Win7, node v4.4.5, npm v3.9.0

Answer

In each router you're calling app.listen. This is what starts the express server listening on that port. It will work for the first router, but on the second it will throw an error telling you that the port is in use. Only one application can listen on a specific port. The correct way to do this is to remove anything common from the individual routers and put them in your app.js