My POST from html returns no value, nodejs, express

I am trying to do a simple post from a html page and when I check the console after post it returns nothing. I am using the server side body parser correctly. Is there something I am doing wrong?

   var http = require('http');
var fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
//var routes = require('./routes');

//****** Mongodb Code *********************************************
var mongodb = require('mongodb');
var Db = require('mongodb').Db;
var assert = require('assert');
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/test';
var mongoose = require('mongoose');
//*****************************************************************

var app = express();
/*
var index = fs.readFileSync('index.html')
var signin = fs.readFileSync('signin.html')
var createaccount = fs.readFileSync('createaccount.html')
var forgotpassword = fs.readFileSync('forgotpassword.html')
var account = fs.readFileSync('account.html')
var upload = fs.readFileSync('uploadphoto.html')
*/

/*
var serv = http.createServer(function (req, res){
    res.writeHead(200, { 'Content-Type': 'text/html; char=utf-8'});
    res.end(index);
})
*/


//  app.set('views',__dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.static(path.join(__dirname)));
    app.use(bodyParser.json())

app.get("/forgotpassword", function(req, res){
    res.render('forgotpassword');
})

app.post("/forgotpassword", function(req,res){
    console.log(req.body);
})

app.listen(3000);

    console.log("Express server listening on port 3000.")

      

below is my html code.

<body>

<div id="header">
    <div id="link">
    <a href="/" align="right">Home</a>
    <a align="right" href="signin">Sign-in</a>
    </div>
    <div id="logo">
    <center><h1><i>IMGCAPTURE</i></h1></center>
    </div>
</div>

<div id="forgotpassword">
    <form id="enteremail" action="forgotpassword" method="POST">
    <center><h1><i>Enter Your Email:</i></h1></center><br><br>
    <center><input id="useremail" name="email" type="email"></input></center><br>
    <center><input type="submit" value="Submit"></input></center>
</form>
</div>
    </body>
    </html>

      

+3


source to share


1 answer


Try to use

app.use(bodyParser.urlencoded({ extended: true }));

      



instead

app.use(bodyParser.json())

      

+2


source







All Articles