admin 管理员组

文章数量: 1086019

Here is my following code of my express application with typescript 3.7.4:

import bodyParser from "body-parser";
import config from "config";
import cookieParser from "cookie-parser";
import express from "express";
import mongoose from "mongoose";
import path from "path";

export default class App {

    public app: express.Application;

    constructor() {
        this.app = express();
        this.initializeMiddlewares();
    }

    public listen(port: any) {
        this.app.listen(port, () => {
            console.log(`App listening on the port ${port}`);
        });
    }

    public initializeControllers(controllers: any) {
        controllers.forEach((controller: any) => {
            this.app
                .use(bodyParser.json())
                .use(bodyParser.urlencoded())
                .use("/", controller.router);
        });
    }

}`

Please, help me with this part of code. I don't understand, why I'm getting undefined in request.body, when i'm sending post request.

Here is my following code of my express application with typescript 3.7.4:

import bodyParser from "body-parser";
import config from "config";
import cookieParser from "cookie-parser";
import express from "express";
import mongoose from "mongoose";
import path from "path";

export default class App {

    public app: express.Application;

    constructor() {
        this.app = express();
        this.initializeMiddlewares();
    }

    public listen(port: any) {
        this.app.listen(port, () => {
            console.log(`App listening on the port ${port}`);
        });
    }

    public initializeControllers(controllers: any) {
        controllers.forEach((controller: any) => {
            this.app
                .use(bodyParser.json())
                .use(bodyParser.urlencoded())
                .use("/", controller.router);
        });
    }

}`

Please, help me with this part of code. I don't understand, why I'm getting undefined in request.body, when i'm sending post request.

Share Improve this question edited Mar 22, 2020 at 13:40 Антон Фесун asked Mar 22, 2020 at 13:32 Антон ФесунАнтон Фесун 311 gold badge1 silver badge3 bronze badges 1
  • I'm having a similar issue. Have you solved this? – Olli Luukas Commented Oct 23, 2020 at 6:42
Add a ment  | 

1 Answer 1

Reset to default 5

If you are running express 4.16 or greater, you no longer need to use body-parser.

You can try doing this instead:

import config from "config";
import cookieParser from "cookie-parser";
import express from "express";
import mongoose from "mongoose";
import path from "path";

export default class App {
    
    public app: express.Application;

    constructor() {
        this.app = express();
        this.initializeMiddlewares();
    }

    public listen(port: any) {
        this.app.listen(port, () => {
            console.log(`App listening on the port ${port}`);
        });
    }

    public initializeControllers(controllers: any) {
        controllers.forEach((controller: any) => {
            this.app.use(express.json());
            this.app.use(express.urlencoded());
            this.app.use("/", controller.router);
        });
    }

}`

See here for more info: https://medium./@mmajdanski/express-body-parser-and-why-may-not-need-it-335803cd048c

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)