Ziqi
Ziqi

Reputation: 2554

node express bodyparser deprecated, new syntax does not work

I am trying to write a simple html page that upon a button click, send a value from a text input field to node server, which then prints on the console of the entered value. know that with express 4 the previous bodyparser is deprecated and following the discussion here: bodyParser is deprecated express 4

But with the following code I still get errors when starting the server:

var express = require('express');
var app = express();

app.use(express.static('resources'));
app.use(express.static(__dirname));
app.use(express.bodyParser.urlencoded());

app.get('/', function (req, res) {
   res.sendFile( __dirname + "/" + "index.html" );
})

app.post('/java_preview', function (req, res) {

   console.log("Request for java_preivew:");
   console.log(req.body.url);
})

var server = app.listen(3000, function () {
  console.log("Server started...")
  var host = server.address().address
  var port = server.address().port

  console.log("Listening at http://%s:%s", host, port)
})

HTML:

$(document).ready(function(){
        $('#previewButton').click(function () {
            //var enterURL=$('inputUrl').val();
            //alert("Button Clicked:"+$('#inputUrl').val());
            $.post("java_preview", {url: $('#inputUrl').val()} );
        });

      });

error log:

/Users/-/Google Drive/papers/ISWC2016_demo/webpages/node_modules/express/lib/express.js:99
      throw new Error('Most middleware (like ' + name + ') is no longer bundle
            ^
Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
    at Function.Object.defineProperty.get (/Users/-/Google Drive/papers/ISWC2016_demo/webpages/node_modules/express/lib/express.js:99:13)
    at Object.<anonymous> (/Users/-/Google Drive/papers/ISWC2016_demo/webpages/startserver.js:6:16)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

any suggestions please

Upvotes: 2

Views: 2692

Answers (3)

user6579503
user6579503

Reputation:

bodyParser is not a part of express anymore. Install it as a dependency with npm, and include it in your project.

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended : false}));

Upvotes: 6

enRaiser
enRaiser

Reputation: 2636

The same answer that you are referring say that you have to use extended: true

If you're still getting a warning with urlencoded you need to use

app.use(bodyParser.urlencoded({
    extended: true
}));
The extended config object key now needs to be explicitly passed, 
since it now has no default value.

Upvotes: 0

David Gatti
David Gatti

Reputation: 3701

Removed the word express from here:

app.use(express.static('resources'));
app.use(express.static(__dirname));
app.use(express.bodyParser.urlencoded());

And everything should work :)

Upvotes: 0

Related Questions