javascript - How to use Body Parser with import and not required? ES6 - Stack Overflow

I am working on a project that uses strictly imports, disabling me from using required statements. I am

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() and bodyParser.urlencoded() are built into Express as express.json() and express.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 expects application/json or application/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 use multipart/form-data. – jfriend00 Commented Feb 15, 2020 at 8:17
 |  Show 4 more ments

1 Answer 1

Reset to default 3

Your 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信