Error ExpressJs POST: TypeError: cannot read property 'title' from undefined

I tried a lot to find out why this error causes an error:

Configuring Listening on 2000 TypeError: Cannot read property 'title' of undefined at /home/abdulsattar/learning/node/express/index.js:9:20 

.Js index:

 var express = require("express"), app = express.createServer(); app.get("/", function(req, res) { res.send('<form action="/new" method="post"><input type="text" name="title" /><input type="submit" /></form>'); }); app.post("/new", function(req, res) { res.send(req.body.title); }); app.configure(function() { console.log("Configuring"); app.use(express.bodyParser()); }); var port = process.env.PORT || 2000; app.listen(port, function() { console.log("Listening on " + port); }); 

I read that express needs bodyParser() . i use d it is higher, but it always fails. I tried it in versions 2.5.8 and 2.5.8 (thinking that this might be a problem), but it failed for both versions. Is something missing?

+4
source share
1 answer

My guess is, try moving the app.configure app in front of your app.get and app.post. The bodyParser middleware is not called. Also, to be safe, add enctype to the form, not necessarily, but independently: application/x-www-form-urlencoded .

Tell me...

+11
source

Source: https://habr.com/ru/post/1397267/


All Articles