I am having trouble with body parser and express as it is not functioning properly. When I print req.body in the console, it returns an empty object.
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine','ejs');
app.get('/' ,function(req,res){
var name = "ganesh"
console.log(req.body.name);
res.render('home',{name : name});
});
app.post('/postname' , function(req,res){
res.redirect("/");
});
app.listen(3000,function(){
console.log("server started");
};
This is the content of my home.ejs file:
<html>
<head>
<title>practice</title>
</head>
<body>
<h1>
hello<%= name %> This is the home page
</h1>
<form action="/postname" method = "POST">
<input type = "text" name="name">
<button>
go
</button>
</form>
</body>
I have ensured that body-parser, ejs, and express are installed correctly.
The following is printed in the console:
Server started {}