Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot POST / error using express

Tags:

I am trying to create a simple form handler using express. I tried the code below for my form:

<form class="form"  action="/" method="post" name="regForm">                   <div class="form-group">         <input type="text" name="username" class="form-control" id="username" placeholder="Username">     </div>     <button type="submit" class="btn btn-default">Submit</button> </form> 

And here is my app.js code:

const port = 3000;  var express = require('express'),     app = express(),     server = require('http').createServer(app); var bodyParser = require('body-parser');  app.use(express.static(__dirname + '/public'));  app.use(bodyParser.urlencoded({    extended: true; }));  app.use(bodyParser.json());  app.post('/',function(req,res){    var username = req.body.username;    var html = 'Hello:' + username;    res.send(html);    console.log(html); });  server.listen(port); 

I keep getting the error "CANNOT POST /" after submitting the form. Am I missing something like a module?

like image 687
dtem052996 Avatar asked Mar 11 '16 02:03

dtem052996


People also ask

What is POST () in Express?

js POST Method. Post method facilitates you to send large amount of data because data is send in the body. Post method is secure because data is not visible in URL bar but it is not used as popularly as GET method. On the other hand GET method is more efficient and used more than POST.

How to use GET and POST method in Node js?

Note: If you are going to make GET, POST request frequently in NodeJS, then use Postman , Simplify each step of building an API. In this syntax, the route is where you have to post your data that is fetched from the HTML. For fetching data you can use bodyparser package. Web Server: Create app.

How to handle GET request?

get() method of the express. js is used to handle the incoming get request from the client-side Basically is it intended for binding the middleware to your application. Parameters: path: It is the path for which the middleware function is being called.


1 Answers

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'));  app.use(bodyParser.urlencoded({    extended: false }));  app.use(bodyParser.json());  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 });  app.post('/',function(req,res){    var username = req.body.username;    var htmlData = 'Hello:' + username;    res.send(htmlData);    console.log(htmlData); });  app.listen(port); 

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

Thanks & Cheers

like image 81
Amulya Kashyap Avatar answered Oct 16 '22 17:10

Amulya Kashyap