7
votes

I'am totaly new to NodeJS in combination with Express. I am trying to make a simple website with Bootstrap components. I have generated the folowing folder structure with the Express generator

  • app.js
  • bin (folder)
  • node_modules(folder)
  • package.json
  • public (folder with: css, javascript, img)
  • routes (index.js, oudRijswijk.js)
  • views (jade files)

Express(version 4.13.1) NodeJS (version 0.10.25)

All works fine until i add some extra routes in the app.js file.

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 nodemailer = require('nodemailer');

var routes = require('./routes/index');
var oudRijswijk = require('./routes/oudRijswijk');

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(app.router);
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);

routes.initialize(app);

app.use('/OudRijswijk', oudRijswijk);

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

After starting the NodeJS server with the command "npm start" in the folder i get the following error:

Error: 'app.router' is deprecated!
Please see the 3.x to 4.x migration guide for details on how to update your app.
    at EventEmitter.Object.defineProperty.get (/home/erik/git/expresswebsite`/node_modules/express/lib/application.js:123:13)`
    at Object.<anonymous> (/home/erik/git/expresswebsite/app.js:22:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/erik/git/expresswebsite/bin/www:7:11)
    at Module._compile (module.js:456:26)
    npm ERR! weird error 8
    npm WARN This failure might be due to the use of legacy binary "node"
    npm WARN For further explanations, please read
    /usr/share/doc/nodejs/README.Debian

I hope you can help me with this problem..

Many thanks

Erik

1
Did you even bother to read the 3.x to 4.x migration guide? If you did, you probably wouldn't need to ask the question :)James
Yeah guide literally tell to remove that line. It took me less than 1 min to find that.Sushant
I am sorry. I read the Migration Guide and removed the line : app.use(app.router); but i got another error: " TypeError: Object function router(req, res, next) { router.handle(req, res, next); } has no method 'initialize'Erik hoeven

1 Answers

17
votes

According to the migration guide , the need to manually do app.use(app.router) has been removed. Now you can use directly (e.g.):

app.get('/' ...);
app.post(...);

without adding the line 'app.use(app.router)' after it.

Best regards, Eduardo