node.js - node express bodyparser deprecated, new syntax does not work -
i trying write simple html page upon button click, send value text input field node server, prints on console of entered value. know express 4 previous bodyparser deprecated , following discussion here: bodyparser deprecated express 4
but following code still errors when starting 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 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 @ 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 + ') no longer bundle ^ error: middleware (like bodyparser) no longer bundled express , must installed separately. please see https://github.com/senchalabs/connect#middleware. @ function.object.defineproperty.get (/users/-/google drive/papers/iswc2016_demo/webpages/node_modules/express/lib/express.js:99:13) @ object.<anonymous> (/users/-/google drive/papers/iswc2016_demo/webpages/startserver.js:6:16) @ module._compile (module.js:460:26) @ object.module._extensions..js (module.js:478:10) @ module.load (module.js:355:32) @ function.module._load (module.js:310:12) @ function.module.runmain (module.js:501:10) @ startup (node.js:129:16) @ node.js:814:3
any suggestions please
bodyparser not part of express anymore. install dependency npm, , include in project.
var bodyparser = require('body-parser'); app.use(bodyparser.urlencoded({extended : false}));
Comments
Post a Comment