Object.anonymous (Express + Middleware)

1
> > node index.js C:\Users\cpsep\Projetos\nodejs\semparar\node_modules\express\lib\express.js:107
>       throw new Error('Most middleware (like ' + name + ') is no longer bundled with Express and must be installed separately. Please
> see https://github.com/senchalabs/connect#middleware.');
>       ^
> 
> Error: Most middleware (like favicon) is no longer bundled with
> Express and must be installed separately. Please see
> https://github.com/senchalabs/connect#middleware.
>     at Function.get (C:\Users\cpsep\Projetos\nodejs\semparar\node_modules\express\lib\express.js:107:13)
>     at Object.<anonymous> (C:\Users\cpsep\Projetos\nodejs\semparar\index.js:22:16)
>     at Module._compile (module.js:570:32)
>     at Object.Module._extensions..js (module.js:579:10)
>     at Module.load (module.js:487:32)
>     at tryModuleLoad (module.js:446:12)
>     at Function.Module._load (module.js:438:3)
>     at Module.runMain (module.js:604:10)
>     at run (bootstrap_node.js:390:7)
>     at startup (bootstrap_node.js:150:9)

My code:

var express = require('express');
var routes = require('./routes');
var trips = require('./routes/trips');
var favicon = require('express-favicon');
var mongoskin = require('mongoskin');
var db = mongoskin.db('mongodb://localhost:27017/semparar?auto_reconnect', {safe:true});

var app = express();
app.use((req,res,next)=>{
    req.db = {};
    req.db.trips = db.collection('trips');
    next();
})

app.locals.appname = 'ProtheusXWebRota';
app.set('port', process.env.PORT || 3000);
app.set('views',__dirname+'/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
  secret: '59B93087-78BC-4EB9-993A-A61FC844F6C9'
}));
app.use(express.csrf());
app.use(require('less-middleware')({
  src: __dirname + '/public', 
  compress: true 
}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
  res.locals._csrf = req.session._csrf;
  return next();
});
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
};
app.param('trip_id', function(req, res, next, tripId) {
    req.db.trips.findById(tripId, function(error, trip){
        // é muito importante verificar erros e resultados vazios   
        if (error) return next(error);
        if (!trip) return next(new Error('Trip is not found.'));
        // se houver dados, guardo no pedido e vou para o próximo meddleware        
        req.task = task;
        return next();
  });
});
// Definição das rotas
app.get('/', routes.index);
app.get('/trips', trip.list);
app.post('/trips', trip.add);
app.del('trips/:trip_id',trip.del);

// Por segurança responder pedidos mal intecionados
app.all('*',(req, res)=>{
  res.send(404);
});

http.createServer(app).listen(app.get('port'), 
  function(){
    console.log('Express server listening on port '
      + app.get('port'));
  }
);
    
asked by anonymous 09.08.2017 / 17:17

1 answer

1

Express has separated some of your plugins / middlewares into separate components. So instead of using only:

app.use(express.cookieParser());

Now you have to import one by one and use:

const cookieParser = require('cookie-parser');
// ...
app.use(cookieParser());

The same applies to other plugins like
 - bodyParser (is now body-parser ),
 - favicon (is now serve-favicon ),
 - logger (now is morgan ), etc.

    
09.08.2017 / 17:22