2

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

2
  • Have you read the error log? It's telling you exactly what to do. Commented Jul 12, 2016 at 12:52
  • sorry i could be missing some basics here, but I do not understand. Do you mean it is related to where 'express' is installed? Commented Jul 12, 2016 at 12:56

3 Answers 3

6

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

1 Comment

It might be worth mentioning that the body-parser package needs to be installed separately :)
0

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

2 Comments

thank you, if you mean 'app.use(bodyparser.urlencoded())' then it creates a new error: ReferenceError: bodyParser is not defined
You need to include it at the beginning of the file :) var bodyParser = require('body-parser'); no inclusion, no fun ;)
0

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.

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.