简体   繁体   English

Node.js POST 请求作为“{}”返回

[英]Node.js POST request coming back as “{}”

Started using node.js and trying the use the POST request with a fetch but when I run it, it just comes back as {} in the console Server side code -->开始使用 node.js 并尝试使用 POST 请求和 fetch 但当我运行它时,它只是作为 {} 在控制台服务器端代码中返回 -->

const express = require("express")
const app = express()



app.listen(3000, () => console.log("listening at 3000"))
app.use(express.static("public"))
app.use(express.json())



app.post("/api", (request,response) => {
    console.log("Made the POST")
    console.log(request.body)
})

the fetch -->获取-->

async function submitValues(){
    const start = document.getElementById("start").value;
    const end = document.getElementById("end").value
    // console.log(start,end)
    // findShortestPath(graph, start, end)
    data = {start, end}
    
    options = {
        method: "POST",
        headers: {
            "Content-Type":"applcation/json"
        },
        body: JSON.stringify(data),
        
    }
    
    await fetch("/api", options);
    
    
}

This is what i've written but just cant figure it out这是我写的,但就是想不通

First you need to install Node.js body parsing middleware:首先需要安装Node.js body解析中间件:

$ npm install --save body-parser

and then:进而:

const bodyParser = require('body-parser');

app.use(bodyParser.json());
app.post("/api", (request,response) => {
    console.log("Made the POST")
    console.log(request.body)
})

This method doesn't return anything, so not sure why you're surprised this API returns nothing.这个方法不返回任何东西,所以不知道为什么你对这个 API 什么都不返回感到惊讶。

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

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