node.js - having issues migrating from express 3.x to 4.x
ringa_lee
ringa_lee 2017-04-17 11:12:13
[Node.js讨论组]
    Failed to load c++ bson extension, using pure JS version

    /home/justhacker/tools/codes/microblog/node_modules/express/lib/express.js:89

    throw new Error('Most middleware (like ' + name + ') is no longer bundle ^

    Error: Most middleware (like session) is no longer bundled with Express and must be

    installed separately. Please see https://github.com/senchalabs/connect#middleware.

    at Function.Object.defineProperty.get (/home/justhacker/tools/codes/microblog/node_modules/express/lib/express.js:89:13)

    at module.exports (/home/justhacker/tools/codes/microblog/node_modules/connect-mongo/lib/connect-mongo.js:30:22)

    at Object. (/home/justhacker/tools/codes/microblog/app.js:14:42)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:349:32)
    at Function.Module._load (module.js:305:12)
    at Function.Module.runMain (module.js:490:10)
    at startup (node.js:124:16)
    at node.js:807:3

官方文档。
Middleware
As of 4.x, Express no longer depends on Connect. All of Express' previously included middleware are now in separate repos. Please view the list of middleware. The only included middleware is now express.static().

但是我的代码里面没有用。

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session'); 
var bodyParser = require('body-parser');

var util = require('util');

var routes = require('./routes/index');
var users = require('./routes/users');
//var connect = require('connect');
var MongoStore = require('connect-mongodb');
var setting = require('./setting');


var app = express();




// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
//app.use(express.methodOverride());
app.use(cookieParser());
app.use(session({ 
secret: setting.cookieSecret, 
store: new MongoStore({ 
db: setting.db 
}) 
})); 
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

/// catch 404 and forwarding 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;

现在报错

Failed to load c++ bson extension, using pure JS version

现在界面显示这个。

Cannot read property 'findOne' of null
undefined

TypeError: Cannot read property 'findOne' of null
at MongoStore.MONGOSTORE.get (/home/justhacker/tools/codes/microblog/nodemodules/connect-mongodb/lib/connect-mongodb.js:115:14)
at Layer.session [as handle] (/home/justhacker/tools/codes/microblog/nodemodules/express-session/index.js:328:11)
at trimprefix (/home/justhacker/tools/codes/microblog/nodemodules/express/lib/router/index.js:226:17)
at /home/justhacker/tools/codes/microblog/nodemodules/express/lib/router/index.js:198:9
at Function.proto.processparams (/home/justhacker/tools/codes/microblog/nodemodules/express/lib/router/index.js:251:12)
at next (/home/justhacker/tools/codes/microblog/nodemodules/express/lib/router/index.js:189:19)
at Layer.cookieParser [as handle] (/home/justhacker/tools/codes/microblog/nodemodules/cookie-parser/index.js:46:5)
at trimprefix (/home/justhacker/tools/codes/microblog/nodemodules/express/lib/router/index.js:226:17)
at /home/justhacker/tools/codes/microblog/nodemodules/express/lib/router/index.js:198:9
at Function.proto.processparams (/home/justhacker/tools/codes/microblog/nodemodules/express/lib/router/index.js:251:12)
ringa_lee
ringa_lee

ringa_lee

全部回复(2)
阿神

express 4.x 分离了middleware,除了static,其他需要自行安装

大家讲道理

Error: Most middleware (like session) is no longer bundled with Express and must be
installed separately.

Please see https://github.com/senchalabs/connect#middleware.

npm install express-session

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号