NodeJS 无法使用Postman发布到Express应用程序

siv3szwd  于 2023-05-06  发布在  Node.js
关注(0)|答案(2)|浏览(123)

我在我的node应用程序中有以下代码:

// POST route
app.post('/bb', async (req, res) => {
    try {
        // Assuming you're receiving JSON data in the request body
        const newData = await req;
        // Process the new data here
        console.log('Received new data:', newData);
        res.status(200).json({ message: 'Data received successfully' });
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

app.listen(3000, () => console.log('Server started on port 3000'));

当我尝试用Postman发送一些JSON时,我得到:

Cannot POST /bb%0A

我做错了什么?

c90pui9n

c90pui9n1#

我在 Postman 的最后一个URL http://localhost:3000/bb[arrow]中看到了箭头,因此您可以删除该箭头并重试。

vlju58qv

vlju58qv2#

看这里

const express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const app = express();

 const port = 5500;

 app.use(cors()); // enable CORS

 app.use(express.json());
  app.use(express.urlencoded({ extended: true }));

 app.post('/test', (req, res) =>{
  console.log(req.body);

  res.json('okey');
 }); 



app.listen(port, () => console.log(`Example app listening on port 
${port}!`));

相关问题