`Nodejs - Express` - 获取错误为“ReferenceError:bodyparser未定义”

时间:2016-03-31 08:13:16

标签: node.js express

我正在调用config中的server.js文件。当我调用config文件时,我收到的错误为ReferenceError: bodyparser is not defined。我不明白我的结局是错的。

有人帮我解决这个问题吗?

这是我的配置文件:

var 

path = require('path'),
routes = require('./routes'),
exphbs = require('express-handlebars'),
express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
morgan = require('morgan'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler');


module.exports = function(app) {

    app.use(morgan('dev'));

    app.use(bodyParser({
        uploadDir:path.join(__dirname, 'public/upload/temp')
    }));

    app.use(methodOverride());

    app.use(cookieParser('some-secret-value-here'));
    routes(app);

    app.use('/public/', express.static(path.join(__dirname, '../public')));

    if ('development' === app.get('env')) {
        app.use(errorHandler());
    }

    return app;
};

server.js:

var express = require('express'),
    config  = require('./server/configure'),
    app     = express();

app
    .set( "port", process.env.PORT || 3300 );

app
    .set( "views", __dirname + '/views');

app = config( app );

// app
//  .get('/', function( req, res ) {

//      res.send( 'Hello World' );

//  } );


app
    .listen( app.get('port'), function () {

        console.log('Server up: http://localhost:' + app.get('port'));

    })

更新

module.exports = function(app) {

    app.use(morgan('dev'));

    app.use(bodyParser.json());

    app.use(bodyParser.urlencoded({ extended: false }));

    app.use(bodyParser({
        uploadDir:path.join(__dirname, 'public/upload/temp')
    }));

    app.use(methodOverride());

    app.use(cookieParser('some-secret-value-here'));
    routes(app);

    app.use('/public/', express.static(path.join(__dirname, '../public')));

    if ('development' === app.get('env')) {
        app.use(errorHandler());
    }

    return app;
};

2 个答案:

答案 0 :(得分:2)

config.js

中添加以下内幕功能
// parse application/json
app.use(bodyParser.json());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse the raw data
app.use(bodyParser.raw());
// parse text
app.use(bodyParser.text());

Reference to bodyParser documentation

更新的代码:

在configure.js

var path = require('path'),
    express = require('express'),
    bodyParser = require('body-parser');
module.exports = function(app) {
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser({
        uploadDir:path.join(__dirname, 'public/upload/temp')
    }));
    return app;
};

在server.js

var express = require('express'),
    config  = require('./configure'),
    app     = express();
    bodyParser = require('body-parser');

app = config( app );


app.post('/about', function( req, res ) {
            console.log(req.body.message);
            res.send( 'Hello World' );
    });
app.listen( 3000, function () {
            console.log('Server up: http://localhost:' + 3000);
        });

答案 1 :(得分:-1)

TypeError::无法读取未定义的属性“ Store”     在新的module.exports(/home/dickens/Desktop/node-login/node_modules/connect-mongo/src/index.js:58:50)     在对象。 (/home/dickens/Desktop/node-login/app.js:56:14)

index.js

change 目录在(/ home / user / your-app / node_modules / connect-mongo / src /index.js:58:50)