Tak
Tak

Reputation: 167

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

Upvotes: 1

Views: 2844

Answers (3)

KansaiRobot
KansaiRobot

Reputation: 10012

Just like this answer it seems that connect has been deprecated so that bodyParser does not work like that anymore. So you have to install it separatedly with npm install body-parser and then use it like var bodyParser = require('body-parser')

Upvotes: 1

Sandeep Bangarh
Sandeep Bangarh

Reputation: 12

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

Upvotes: 0

UtkarshPramodGupta
UtkarshPramodGupta

Reputation: 8152

You must be using the lastest connect module. In the connect 3+ versions, you don't have bodyParser method anymore. It has been moved to a completely different package of its own called body-parser. Read this.

Upvotes: 2

Related Questions