javascriptnode.jsbody-parser

connect.bodyParser is not a function


I am completely new to node.js. I am trying to use bodyParser in connect module, but the error message says TypeError: connect.bodyParser is not a function. My code (simplified version) is below; What can I use instead of connect.bodyParser?

  var connect = require('connect');
  var util = require('util');
  var form = require('fs').readFileSync('form.html');

  var app = connect()
    .use(connect.bodyParser())
    .use(connect.limit('64kb'))
    .use(function(req, res){
      if(req.method === 'POST'){
        res.end(util.inspect(req.body));
      }
      if(req === 'GET'){
        res.writeHead(200, {'Content-Type' : 'text/html'});
        res.end(form);
      }
    }).listen(3000);

Solution

  • Use it Like this:-

    var express = require('express');
    //console.log(express.address());
    var router = express.Router();
    var bodyParser = require('body-parser');
    var bcrypt = require('bcryptjs');
    var ObjectId = require('mongodb').ObjectID;
    
    router.use(bodyParser.urlencoded({ extended: false }));
    router.use(bodyParser.json());