Most middleware (like favicon) is no longer bundled with Express
Error: Most middleware (like favicon) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
Solution:
For Express 4 :
// Serve favicon.ico.
$> npm install serve-favicon
Then in your server .js script :
var favicon = require(‘serve-favicon’);
app.use(favicon(options.favicon));
For Express 3 :
This is likely the syntax that gave you the error above
app.use(express.favicon(options.favicon));
---------------------------------------------------------------------------------
/Users/user/Desktop/workspace150302/WeDateServer/node_modules/serve-favicon/index.js:45
if (!path) throw new TypeError('path to favicon.ico is required');
^
TypeError: path to favicon.ico is required
改为"static-favicon"
:
"~1.0.0"
,就可以了
--------------------------------------------------------------------------------------
Error: Most middleware (like session) is no longer bundled with Express and must be installed separately.
最后通过搜索该错误解决方案,发现是因为网上的方法适用于express3.x,express4.0+已经不再内置session,需要另加中间件。
我的package.json版本
{
"name": "WeDateServer",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"express": "~4.9.8",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"jade": "~1.6.0",
"ejs": "~2.3.1",
"mongodb": "~1.4.32",
"connect-mongo":"~0.7.0",
"node-uuid": "~1.4.2",
"formidable": "~1.0.17",
"gridform":"~0.1.7",
"nodemailer":"~1.3.1",
"generic-pool":"~2.1.1",
"amqp":"~0.2.3",
"nodeunit": "~0.9.0",
"request": "~2.53.0",
"express-session":"1.10.3",
"cookies":"0.5.0",
"static-favicon":"2.0.0"
}
}
app.js通过测试的代码:
var express = require('express'); var path = require('path'); var favicon = require('static-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var session = require('express-session'); var http = require('http'); var settings = require('./settings'); var MongoStore = require('connect-mongo')(session); var fs = require('fs'); var accessLogfile = fs.createWriteStream('access.log',{flags:'a'}); var errorLogfile = fs.createWriteStream('error.log', {flags:'a'}); var amqp = require('amqp'); var domainMiddleware = require('./lib/domain.js'); var routes = require('./routes/index'); var server = http.createServer(); var app = express(); app.use(favicon()); app.use(domainMiddleware({ server:server, killTimeout: 30000 })); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('env','production'); 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(session({ secret: settings.cookieSecret, //store: new MongoStore(settings.replicaSet), //settings.singleServer for localhost store : new MongoStore({ db : settings.db, host : '127.0.0.1', port : '27017' }), cookie: { expires: new Date(Date.now() + 60 * 1000), maxAge: 60*1000 } })); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); /* connect to rabbitMQ, create a exchange( routKey: A--APNS E--Email) to push message to APNS queue or Emai queue */ app.rabbitMQConnection = amqp.createConnection({host:settings.rabbitMQHost, port:settings.rabbitMQPort, login:settings.rabbitMQUser, password:settings.rabbitMQPassword}); app.rabbitMQConnection.on('ready',function(){ console.log('connect to the rabbitMQ successful'); app.rabbitMQConnection.exchange('router',{type: 'direct',autoDelete: false,confirm: true},function(exchange){ app.e = exchange; }); }); // error handlers // development error handler // will print stacktrace if (app.get('env') === 'development') { // app.use(express.errorHandler()); app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } //production only if ('production' == app.get('env')) { app.use(logger({stream:accessLogfile})); app.use(function(err, req, res, next){ var meta = '['+ new Date() +']' + req.url + '\n'; errorLogfile.write(meta + err.stack + '\n'); next(); }); } // 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: {} }); }); exports.server= server; module.exports = app;
express 4.x 分离了middleware,除了static,其他需要自行安装