node.js - `Nodejs - Express` - 出现错误 `ReferenceError: bodyparser is not defined`

标签 node.js express

我正在从 server.js 调用 config 文件。当我调用 config 文件时,我收到错误 ReferenceError: bodyparser is not Define。我不明白我的结局有什么问题。

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

这是我的配置文件:

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

服务器.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;
};

最佳答案

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

关于node.js - `Nodejs - Express` - 出现错误 `ReferenceError: bodyparser is not defined`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327702/

相关文章:

javascript - 我必须履行我的 JavaScript promise 吗?

node.js - 风 sails : how to redirect after creating a model with the built-in crud operations?

node.js - Node Express RESTful API默认引擎用于错误处理

Node.js + Express + 样板 + jquery 模块不起作用

javascript - 按当前时间戳每分钟更新一次 Node.js Express api

javascript - TypeError : db. collection is not a function in MongoDB Connection

node.js - 在 Nodejs Express 4 中通过单个输入上传多个文件的最佳方法是什么?

javascript - req.body 如何正确映射到这里的 const 变量数组?

javascript - Passport.js 注册似乎有效,但用户凭据未插入数据库

javascript - 无法使用 Restangular 获取快速删除中的 json 请求?