3gwebtrain
3gwebtrain

Reputation: 15293

`Nodejs - Express` - getting error as `ReferenceError: bodyparser is not defined`

I am calling the config file from server.js. when i call the config file, I am getting the error as ReferenceError: bodyparser is not defined. I don't understand the wrong with my end.

any one help me to sort this?

here is my config file :

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

    })

update

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

Upvotes: 5

Views: 26143

Answers (2)

Dickens
Dickens

Reputation: 33

TypeError: Cannot read property 'Store' of undefined at new module.exports (/home/dickens/Desktop/node-login/node_modules/connect-mongo/src/index.js:58:50) at Object. (/home/dickens/Desktop/node-login/app.js:56:14)

change directory of your index.js in the (/home/user/your-app/node_modules/connect-mongo/src/index.js:58:50)

Upvotes: -1

Himani Agrawal
Himani Agrawal

Reputation: 1272

Add following inside function in 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

Updated code:

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

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

Upvotes: 4

Related Questions