Nodejs 服务器部署的 node 服务,为什么 postman 请求不了
遇到一个非常诡异的问题,还请各位大佬指点
在阿里云服务器上用 pm2 部署了一个 node 项目。 本地的一个 vue 代码直接去访问这个 api.xxx.xxxx 的接口可以正常访问通。
但是用 postman 去访问这个接口的时候,访问不通,postman 页面:Could not get response
这是什么原因,有遇到过相同问题的吗?
Nodejs 服务器部署的 node 服务,为什么 postman 请求不了
条件太少了,可能代理问题、或者 postman 请求拼的不对吧
可以用 postman 你有问题的请求,生成一个 fetch 的代码,去你 Vue 的页面下执行一下,看看是不是报错,报错了大概率 postman 请求拼的不对。
有没有可能是 vue 项目,中配置的 proxy 把请求路径转化了
还是很诡异,浏览器直接输入 api.xxxx 这个接口地址是有返回的,本地项目也可以直接访问,就是在 postman 中就没有响应,我的 ngxin 配置和 ssl 配置都是新的,难道是因为 ssl 原因吗
请求都对,就是没有反应,还没找到是什么原因。。
我用 apifox 去访问这个接口也是可以通的,就 postman 不可以。。。
都转成 curl 命令对比一下
Postman 有没有关闭 SSL certificate verification ?
关闭了的
用 curl 在服务器上执行返回是正确的,本地命令后执行,
curl -v POST https://api.xxx/login <br> -H “Content-Type: application/json” <br> -d '{“username”:“test”, “password”:“test”}'
直接返回了这个错误
* Could not resolve host: POST
* Closing connection
curl: (6) Could not resolve host: POST
* Host api.yxdjjz.cn:443 was resolved.
* IPv6: (none)
* IPv4: 47.95.68.15
* Trying 47.95.68.15:443…
* Connected to api.yxdjjz.cn (47.95.68.15) port 443
* ALPN: curl offers h2,http/1.1
* (304) (OUT), TLS handshake, Client hello (1):
* CAfile: /etc/ssl/cert.pem
* CApath: none
* Recv failure: Connection reset by peer
* LibreSSL/3.3.6: error:02FFF036:system library:func(4095):Connection reset by peer
* Closing connection
curl: (35) Recv failure: Connection reset by peer
本地浏览器和本地项目都是可行的,curl 本地命令后就出现这个问题了
突然想到,ssl 证书是申请的个人免费版本,难道跟这个有关系?
更新下客户端证书
postman 里面添加个人证书吗?直接是关闭证书校验了,我觉得是我配置的有问题,但是又能访问,,,
#10
curl -v -x POST …
POST 这个参数删掉应该也成
curl -v -k -X POST https://api.xxx/login <br> -H “Content-Type: application/json” <br> -d ‘{“username”:“test”, “password”:“test”}’
你用北京阿里云没做备案? powershell7 执行下代码可以更直观看到原因Invoke-WebRequest -Method GET -Uri <a target="_blank" href="http://api.yxdjjz.cn/login" rel="nofollow noopener">http://api.yxdjjz.cn/login</a> -SkipHttpErrorCheck -OutFile res.html && Invoke-Item res.html
好像就是因为域名没备案的原因 感谢感谢🙏
以为是 Bug ,结果是天朝的 Feature (
既然没备案,那浏览器凭啥是成功的呢??
是啊 凭啥,客服说是因为不稳定什么因素
在 Node.js 服务器上部署 node 服务后,如果 Postman 无法请求,可能有几个常见的原因和相应的解决方法。以下是一些排查步骤和示例代码:
-
检查服务器是否启动: 确保你的 Node.js 服务器已经正确启动,并且监听在正确的端口上。
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
-
检查防火墙设置: 确保没有防火墙规则阻止访问你的服务器端口。
-
CORS(跨源资源共享)问题: 如果你的前端和后端部署在不同的域或端口上,确保后端配置了 CORS 头部。
const cors = require('cors'); const app = require('express')(); app.use(cors()); app.get('/', (req, res) => { res.json({ msg: 'This is CORS-enabled for all origins!' }); }); app.listen(3000, () => { console.log('CORS-enabled web server listening on port 3000'); });
-
Postman 请求设置: 确保在 Postman 中设置了正确的请求 URL、方法(如 POST)、头部和体。
通过以上步骤,你应该能够诊断并解决 Postman 无法请求 Node.js 服务器的问题。