I use ExpressJs to create a simple app, receiving a post request (also allow handling form submission).
My index.js file:
require ('./models/db');
const express = require('express')
const app = express()
const bodyParser = require('body-parser');
const hotelController = require('./controllers/hotelController');
app.use(express.static('public'))
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('/', (req, res)=>{
res.sendFile(path.join(__dirname, public, 'index.html'));
})
app.listen(30001, () => console.log("listen on 30001"))
app.use('/hotel', hotelController);
Below is the controller file, nothing special, just accepting a POST request, however, when I tried logging out the request, I just see empty bracket {}
, and the code after that breaks. The body of the request is empty, regardless posting from a form or using postman
const express = require ('express');
var router = express.Router();
const blagoose = require ('mongoose');
const Hotel = blagoose.model('Hotel');
router.post('/',(req, res)=> {
console.log(req.body)
try {
//if (req.body._id === "")
insertRecord(req,res);
// else
//updateRecord(req,res);
} catch (error) {
console.log("some error just happen " + error.message)
}
})
//some more code
module.exports = router;
Edit 2: So, after ozgur's tip on checking Postman header (changing Content-Type to application/x-www-form-urlencoded ), the Postman request works (it has a good body). However, I tried to handle from the form, it becomes empty again. Here's the code in my index.html file (I know it's messy when javascript and html are in the same file, bear with me on that).
<form name="formInfo" autocomplete="off" enctype="application/x-www-form-urlencoded" >
<div class="form-group">
<label for="inputOwnerName">Name </label>
<input type="text" class="form-control" id="inputOwnerName" placeholder="Some name" value="Test Name" >
<div class="form-group" style="text-align: center;">
<button class="btn btn-yellow" style="width: 200px;">Send</button>
</div>
</form>
<script>
var form = document.forms.namedItem("formInfo");
form.addEventListener('submit', function(ev) {
// var oOutput = document.querySelector("div");
var oData = new FormData(form);
var ownerName = document.getElementById("inputOwnerName").value;
alert(ownerName);
oData.append("inputOwnerName", ownerName);
var oReq = new XMLHttpRequest();
oReq.open("POST", "/hotel", true);
oReq.onload = function(oEvent) {
if (oReq.status == 200) {
// oOutput.innerHTML = "Uploaded!";
} else {
// oOutput.innerHTML = "Error " + oReq.status + " occurred when trying to upload your file.<br \/>";
}
};
oReq.send(oData);
ev.preventDefault();
}, false);
</script>
Can you please change your code and instead of this
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
Just use this:
app.use(express.json());
And in Postman, use this content type: "application/json"
and try submitting with Raw data
EDIT: my default post headers for json data
First swap these two lines with each other
app.listen(30001, () => console.log("listen on 30001"))
app.use('/hotel', hotelController);
Next the reason that its empty is that controller is not defined correctly ie where did you export
the router
object from controller? Try this:
---
module.exports = router
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.