node.js - having issues migrating from express 3.x to 4.x
ringa_lee
ringa_lee 2017-04-17 11:12:13
0
2
691
    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,其他需要自行安裝

大家讲道理

錯誤:大多數中間件(如 會話不再與 Express 捆綁必須
單獨安裝。

請參閱https://github.com/senchalabs/connect#middleware.

npm install express-session

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板