5

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;
};
5
  • can you show you server.js file ? Commented Mar 31, 2016 at 8:26
  • server.js - added. see my question Commented Mar 31, 2016 at 8:29
  • Is it installed and included in node_modules? Commented Mar 31, 2016 at 9:12
  • yes. it is installed. that's what i am wondering here Commented Mar 31, 2016 at 9:15
  • It's weird that bodyparser in error is lowercase. At what line you get this error? Commented Mar 31, 2016 at 12:44

2 Answers 2

4

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);
        });
Sign up to request clarification or add additional context in comments.

7 Comments

Still i am getting the same error. for the updated code see my update in quesiton
How are you sending the data in request ?
can you download from here and see : dropbox.com/s/0ew7h43qvhhcy5r/NodeJs.7z?dl=0
The answer is updated for parsing other formats. Does anyone those work? To check you can console.log(req.body.username) if username is the parameter send in body.
I updated yours and tried, while starting the node itself i am getting error
|
-1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.