Question I'm trying to create a Node.js API, when I write a server.js file, my code looks like this:
var express = require('express'); var express = require('body-parser'); var app = express(); app.use(bodyParser.json()); app.get('/api/posts', function(req,res) { res.json([ { username: 'oscar', body: 'hello' } ]) }) app.listen(3000, function() { console.log('Server Listening on', 3000) })
However, on the command line, I get this error:
body-parser deprecated bodyParser: use individual json.urlencoded middlewares server.js:4:11 body-parser deprecated undefined extended: provide extended option node_modules\body-parser\index.js:85:29
I tried changing this to:
app.use(bodyParser.urlencoded({ extended: true }));
and
app.use(bodyParser.urlencoded({ extended: false }))
like other posts, but it still gives the same error. Not sure what to do now! Please, help.
Thanks!
Richard WU
source share