简体   繁体   English

在 expressjs 应用程序中,如何在 API 端点中处理带有 POST 请求的数组?

[英]In expressjs application how to handle array with POST request in API endpoint?

I'm trying to build expressjs API endpoint.我正在尝试构建 expressjs API 端点。 For my application purpose object will come to another server and it's multiple object.对于我的应用程序目的,object 将来到另一台服务器,它是多个 object。 I need to convert it an array and after that I need to push it to my database or others.我需要将它转换为一个数组,然后我需要将它推送到我的数据库或其他人。

Also I'm using express 4+ so that's why I didn't use body-parser because it's included with express.另外我使用的是 express 4+,所以我没有使用 body-parser,因为它包含在 express 中。

###Note: Data comes from another server and it's "application/x-www-form-urlencoded" format. ###注意:数据来自另一台服务器,格式为“application/x-www-form-urlencoded”。

I expect data will be like this static data:我预计数据会像这样 static 数据:

    [
        {
            "name": "As Md Habibullah",
            "phone": "+457578424",
            "email": "fhhgssa@yahoo.com",
            "car_brand": "Mercedes-Benz",
            "car_number": "256858"
        },
        {
            "name": "Filippo Masiero",
            "phone": "+hgfhfhfg",
            "email": "ghjhghjhg@yahoo.com",
            "car_brand": "Mercedes-Benz",
            "car_number": "25586458"
        },
        {
            "name": "Azad Ahmed",
            "phone": "+ghjggffg",
            "email": "jghjh@yahoo.com",
            "car_brand": "Mercedes-Benz",
            "car_number": "2566868"
        },
        {
            "name": "Md Musa",
            "phone": "+fgjhfgjfh",
            "email": "dfgyfyhfggh@yahoo.com",
            "car_brand": "Mercedes-Benz",
            "car_number": "256858"
        }
    ]

I assume data will comes to my endpoint, it's multiple object and I need to do an array like multiple object: {}, {}, {}我假设数据将到达我的端点,它是多个 object,我需要做一个像多个 object 之类的数组: {}, {}, {}

I want to do an array like: [{}, {}, {}, {}]我想做一个数组,如: [{}, {}, {}, {}]

Is it possible?可能吗?

If the data comes as JSON, but clothed in the application/x-www-form-urlencoded , content-type, then you will have to use the express middleware for that content-type.如果数据以 JSON 的形式出现,但包含在application/x-www-form-urlencoded内容类型中,那么您将不得不为该内容类型使用 express 中间件。 That will give you decoded JSON as plain text which you will then have to manually parse into a Javascript object like this:这将使您将 JSON 解码为纯文本,然后您必须手动将其解析为 Javascript object ,如下所示:

// middleware for parsing application/x-www-form-urlencoded content-type 
app.use(express.urlencoded({extended: true}));

app.post("/somerouter", (res, res) => {
    try {
        // parse decoded body text from JSON into Javascript object
        let obj = JSON.parse(req.body);
        console.log(obj);
        // process obj here
        res.send("ok");
    } catch(e) {
       console.log("bad JSON", e);
       res.sendStatus(400);
    }
});

Ideally, the request would be sent as application/json and then you can use the express.json() middleware and the JSON will automatically be parsed into your object.理想情况下,请求将作为application/json发送,然后您可以使用express.json()中间件,JSON 将自动解析为您的 object。


But, if your application/x-www-form-urlencoded data is just form data, then it is already parsed for you and is already in req.body as properties on that object.但是,如果您的application/x-www-form-urlencoded数据只是表单数据,那么它已经为您解析并且已经在req.body中作为 object 的属性。 You just access it directly in req.body :您只需在req.body中直接访问它:

// middleware for parsing application/x-www-form-urlencoded content-type 
app.use(express.urlencoded({extended: true}));

app.post("/somerouter", (res, res) => {
    console.log(req.body);
    // process data in req.body and then send some response
    res.send("ok");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM