Sunday, 2 April 2017

req.body returning empty object (I've included bodyparser)

I'm new to backend javascript and I have a form that on submit is supposed to show the submitted object, but it's displaying an empty object instead. I've included body-parser so I'm not sure where the issue is. Here is my app.js file:

const express = require("express");
const app = express();
const mustacheExpress = require("mustache-express");
const bodyParser = require("body-parser");
const pgp = require("pg-promise")();

app.engine("html", mustacheExpress());
app.set("view engine", "html");
app.set("views", __dirname + "/views"); 
app.use("/", express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get('/new_creature',function(req,res){
    res.render('new_creature/index');
});
app.post('/new_creature', function(req,res){
    res.end(JSON.stringify(req.body));
    console.log(req.body);
    // res.render('new_creature/index');
});

and here is my views/creatures/new_creature/index.html file:

<form method="POST" action="/new_creature">
    Species:<input type="text">
    Family:<input type="text">
    Habitat:<input type="text">
    Diet:<input type="text">
    Planet:<input type="text">
    <input type="submit" placeholder="submit">
</form>



via Moe Chughtai

No comments:

Post a Comment