Object.<Anonymous> (Express + Middleware)

Asked

Viewed 644 times

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'));
  }
);

1 answer

1


express has separated some of its plugins/middlewares into separate components. So, you can use 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 (now is the body-parser),
- favicon (now is the serve-favicon),
- logger (now is the morgan), etc..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.