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);
'''
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);
'''
Share Improve this question asked Feb 15, 2020 at 7:38 Tristin BTristin B 1171 gold badge1 silver badge9 bronze badges 9-
1
So,
bodyParser.json()
andbodyParser.urlencoded()
are built into Express asexpress.json()
andexpress.urlencoded()
so you don't have to import body-parser at all. – jfriend00 Commented Feb 15, 2020 at 7:47 - In terms of my code, '''app.use(express.json()); app.use(express.urlencoded({extended: true})); ''' should work then? And then I can get the contents using req.body? – Tristin B Commented Feb 15, 2020 at 7:55
- Yes, that should work. – jfriend00 Commented Feb 15, 2020 at 7:58
- Yeah, well it doesn't. I currently have ''app.use(express.json()); app.use(express.urlencoded()); app.use(express.static("public")); app.post("/", function(req, res) { console.log(req.body); console.log(req.body.trackedName); }); '' and req.body is {} although the input was not empty. – Tristin B Commented Feb 15, 2020 at 8:03
-
2
You are apparently using
multipart/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
. – jfriend00 Commented Feb 15, 2020 at 8:17
1 Answer
Reset to default 3Your form is using multipart/form-data
as the content-type, but you don't have any middleware for that content-type and there's no reason to use that more plicated content-type unless you are also uploading files. You can switch the content-type to either one of the two that your middleware does support, application/json
or application/x-www-form-urlencoded
so it will match your middleware.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742334440a4424368.html
评论列表(0条)