I am working on a project that uses strictly imports, disabling me from using required statements. I am wondering how I can read the contents of a post request to my server using the import for Body Parser.
'''
//jshint esversion:6
// Require the needed NPMs
import Radar from "radar-sdk-js";
import express from "express";
import bodyParser from "body-parser";
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
/*app.use(bodyParser.json({
verify: (req, res, buf) => {
req.rawBody = buf;
}
})*/
app.get("/", function(req, res) {
res.sendFile(__dirname + "/public/index.html");
});
app.post("/", function(req, res) {
console.log(req.body);
console.log(req.body.trackedName);
'''
bodyParser.json()
andbodyParser.urlencoded()
are built into Express asexpress.json()
andexpress.urlencoded()
so you don't have to import body-parser at all. – Josmultipart/form-data
instead of one of the two content types you have middleware for? This just requires some debugging on your part to look at the actual incoming request, see the body, see the content-type and then figure out why it isn't being matched by one of your middleware handlers. – Josjson
orurlencoded
. – Praguemultipart/form-data
. I don't know why. But, you have NO middleware for that content type. Your middleware expectsapplication/json
orapplication/x-www-form-urlencoded
. The content-type you are using would require a different middleware as it's a different format. Unless you are uploading files, there is no reason to usemultipart/form-data
. – Jos