javascript - "Can' t POST w/nodejs

标签 javascript node.js express post postman

我只是想用 postman 制作一个app.post req.,但老实说我不知道​​出了什么问题,代码如下:

const express = require('express');

const PORT = 8080;
const HOST = '0.0.0.0';
const app = express();

app.listen(PORT, HOST);

console.log(`Running on http://${HOST}:${PORT}`);

//我想做什么:

**app.post('/test',(req, res) => {
    res.status(200).send('Hello World')
});**

这是我正在做的事情,一步一步:

  • npm start (在终端,是的,进入项目目录) 终端显示如下:

           *> node src/index.js
           Running on http://0.0.0.0:8080*
    
  • 然后,在 Postman 打开的情况下,我使用带有以下链接的“post”方法: <强> http://0.0.0.0:8080/test

重点是,在 postman 身上,我得到了这样的回复:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>Cannot POST /test</pre>
</body>

</html>

而不是这个:

Hello World

所以...我真的不知道出了什么问题...

最佳答案

这里必须注意这两点:

  • 127.0.0.1 是环回地址(也称为 localhost)。
  • 0.0.0.0 是不可路由的元地址,用于指定无效、未知或不适用的目标(“无特定地址”占位符)。

将 HOST 更改为 127.0.0.1 将解决您面临的问题。

您可以阅读更多有关两者之间差异的信息right here

关于javascript - "Can' t POST w/nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59935032/

相关文章:

node.js - 将数组与包含对象的集合数组进行比较

node.js - 在授权路由中使用 express.static 中间件

javascript - 从绑定(bind) formatCurrency() 调用自定义函数时出现错误

javascript - 如何构建与溢出 : auto? 一起使用的可调整大小的 DIV

javascript - Yii2 和 Ajax : The response is not the SQL query results

node.js - 如何在某个日期之前安装 Node 包及其依赖项?

node.js - 为什么 mongoDB 不会更新我的用户条目的 “Date” 字段?

javascript - Internet Explorer 中的快速 session

mysql - 哪些可能的原因会导致nodejs中的mysql队列达到限制问题

javascript - Node : chaining exec commands with promises