Cannot POST / error using express

This way you should try

const port = 3000;

var express = require('express'),
    app = express();
var bodyParser = require('body-parser');

app.use(express.static(__dirname + '/public'));

   extended: false


app.get("", function(req, res){
  res.render('form');// if jade
  // You should use one of line depending on type of frontend you are with
  res.sendFile(__dirname + '/form.html'); //if html file is root directory
 res.sendFile("index.html"); //if html file is within public directory
   var username = req.body.username;
   var htmlData="Hello:" + username;


Things you should keep in mind for future Ref :

  1. You were extending url encode to true
  2. You were not having any get request for your form
  3. You were using HTML named variable which is one of bad practices

Leave a Comment