Postman 卡在“GET”请求中,在使用 PostgreSQL 作为后端的 Node JS 中给出“无法获得响应错误:读取 ECONNRESET”的错误
Posted
技术标签:
【中文标题】Postman 卡在“GET”请求中,在使用 PostgreSQL 作为后端的 Node JS 中给出“无法获得响应错误:读取 ECONNRESET”的错误【英文标题】:Postman is stuck at a "GET" request, giving error that "Could not get response Error: read ECONNRESET" in Node JS using PostgreSQL as backend 【发布时间】:2021-10-19 23:41:21 【问题描述】:我正在关注有关使用 JWT 令牌进行登录身份验证的视频教程。
我在 PostgreSQL 上建立了一个数据库,将它与我的 Node JS 服务器连接起来,并测试了注册和登录命令。这两个查询都在 Postman 上运行良好,并且 Postman 返回了预期的结果。
现在,在登录时,我会在返回值中获得一个 JWT Token,该值将用于进一步获取已登录的用户 ID 及其信息。
因此,为了测试该令牌是否有效,我创建了一个路由来验证用户是否已登录。 (在登录和注册路径所在的同一个文件中添加)
这个路由文件文件的代码如下(jwAuth.js)。 我没有在里面添加注册和登录代码
const router = require('express').Router()
const pool = require("../db")
const bcrypt = require("bcryptjs");
const jwtGenerator = require('../utils/jwtGenerator');
const authorization = require('../middleware/authorization');
// Registeration and Login Routes
// Verification Route
router.get("/is-verify", authorization, async (res, req) =>
try
res.json(true);
catch (err)
console.log(err.message);
res.status(500).send("Server Error")
)
module.exports = router;
中间件authorization.js的代码如下,
const jwt = require("jsonwebtoken");
require("dotenv").config();
module.exports = async (req, res, next) =>
try
const jwtToken = req.header("token");
if (!jwtToken)
return res.status(403).json("Not Authorized");
// In case we do have a token, check if the token is valid or not
const payload = jwt.verify(jwtToken, process.env.jwtSecret);
req.user = payload.user;
catch (err)
console.log(err.message);
return res.status(403).json("You are not authorized");
我还在另一条路线上尝试了同样的事情,dashboard.js 应该使用给它的 JWT 令牌返回/打印用户的 ID,条件是用户正确登录
dashboard.js的代码如下,
const router = require('express').Router()
const pool = require("../db")
const authorization = require("../middleware/authorization")
router.get("/", authorization, async (req, res) =>
try
res.json(req.user);
catch (err)
console.log(err.message)
res.status(500).json("Server Error");
)
module.exports = router
而我的index.js(或server.js)的代码如下,
const express = require('express')
const app = express()
const cors = require('cors')
app.use(express.json()) // Lets you use req.body
app.use(cors())
// ROUTES
// Register and Login Routes
app.use("/auth", require("./routes/jwAuth"));
app.use("/dashboard", require("./routes/dashboard"));
app.listen(3000, () =>
console.log("Console is running");
)
我的问题是,如果我尝试使用所需数据向以下链接发送请求,Postman 工作正常,并且数据也会添加到我的数据库中(并且在检查登录时也可以正确检索)
http://localhost:3000/auth/register
http://localhost:3000/auth/login
但是当我用这个 URL 尝试同样的事情时(通过在 Postman GET 请求的标头中提供 JWT 令牌,
http://localhost:3000/dashboard
http://localhost:3000/auth/is-verify
Postman 卡住了,处理请求花费了很长时间,最终给出了这个错误,
无法得到回应 错误:读取 ECONNRESET
我尝试搜索此错误,显然此问题的解决方法是在 etc/hosts 文件中添加 localhost 的 IP 地址,但在这种情况下,我的注册和登录 POST 请求 也不行。我无法弄清楚问题,所以请有人帮忙。
为了确定,我也在 etc/hosts 文件和注册/登录路由以及 jwtGenerator 文件下方共享了
etc/hosts 文件
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
0.0.0.0 hss.hsselite.com
0.0.0.0 www.hss.hsselite.com
0.0.0.0 d1v9mrqde8r3oj.cloudfront.net
0.0.0.0 www.d1v9mrqde8r3oj.cloudfront.net
0.0.0.0 api.hsselite.com
0.0.0.0 www.api.hsselite.com
0.0.0.0 hsselite.com/trial/step2.php
0.0.0.0 www.hsselite.com/trial/step2.php
0.0.0.0 anchorfree.com
0.0.0.0 www.anchorfree.com
0.0.0.0 box.anchorfree.net
0.0.0.0 www.box.anchorfree.net
0.0.0.0 rpt.anchorfree.net
0.0.0.0 www.rpt.anchorfree.net
0.0.0.0 123.box.anchorfree.net
0.0.0.0 www.123.box.anchorfree.net
0.0.0.0 anchorfree.us
0.0.0.0 www.anchorfree.us
0.0.0.0 delivery.anchorfree.us/land.php
0.0.0.0 www.delivery.anchorfree.us/land.php
0.0.0.0 rss2search.com
0.0.0.0 www.rss2search.com
0.0.0.0 mefeedia.com
0.0.0.0 www.mefeedia.com
0.0.0.0 a433.com
0.0.0.0 www.a433.com
0.0.0.0 techbrowsing.com
0.0.0.0 www.techbrowsing.com
0.0.0.0 techbrowsing.com/away.php
0.0.0.0 www.techbrowsing.com/away.php
0.0.0.0 update.mydati.com
0.0.0.0 www.update.mydati.com
0.0.0.0 mydati.com
0.0.0.0 www.mydati.com
0.0.0.0 geo.mydati.com
0.0.0.0 www.geo.mydati.com
0.0.0.0 updateeu.mydati.com
0.0.0.0 www.updateeu.mydati.com
0.0.0.0 east.mydati.com
0.0.0.0 www.east.mydati.com
0.0.0.0 west.mydati.com
0.0.0.0 www.west.mydati.com
0.0.0.0 us.mydati.com
0.0.0.0 www.us.mydati.com
0.0.0.0 eu.mydati.com
0.0.0.0 www.eu.mydati.com
0.0.0.0 myd3.mydati.com
0.0.0.0 www.myd3.mydati.com
0.0.0.0 ns2.mydati.com
0.0.0.0 www.ns2.mydati.com
0.0.0.0 ns1.mydati.com
0.0.0.0 www.ns1.mydati.com
jwtGenerator.js 文件
const jwt = require('jsonwebtoken');
require('dotenv').config();
function jwtGenerator(user_id)
const payload =
user: user_id
return jwt.sign(payload, process.env.jwtSecret, expiresIn: "1h")
module.exports = jwtGenerator;
jwAuth.js 完整代码
const router = require('express').Router()
const pool = require("../db")
const bcrypt = require("bcryptjs");
const jwtGenerator = require('../utils/jwtGenerator');
const authorization = require('../middleware/authorization');
// Registeration
router.post("/register", async (req, res) =>
try
// 1. Destructure the req.body (name, email, password)
const name, email, password = req.body;
// 2. Check if the user exists (If user already exists, then throw error)
const user = await pool.query("SELECT * FROM users WHERE user_email = $1", [email]);
if (user.rows.length !== 0)
return res.status(401).send("User Already Exists");
// 3. Bcrypt the user password
const saltRound = 10;
const salt = await bcrypt.genSalt(saltRound);
const bcryptPassword = await bcrypt.hash(password, salt);
// 4. Enter the new user inside our database
const newUser = await pool.query("INSERT INTO users (user_name, user_email, user_password) values ($1, $2, $3) RETURNING *", [name, email, bcryptPassword]);
// 5. Generating Our JWT Token
const token = jwtGenerator(newUser.rows[0].user_id);
res.json(token);
catch (err)
console.log(err.message);
res.status(500).send(`Server Error $err`);
)
// Login Route
router.post("/login", async (req, res) =>
try
// 1. Destructure the req.body
const email, password = req.body;
// 2. Check if user does not exist (Throw error if user does not exist)
const user = await pool.query("Select * from users where user_email = $1", [email]);
if (user.rows.length === 0)
res.status(401).send("Email does not Exist");
// 3. Check if the password is same for the user in database
const validPassword = await bcrypt.compare(password, user.rows[0].user_password);
if (!validPassword)
return res.status(401).send("Password is incorrect");
// 4. Give them a JWT Token
const token = jwtGenerator(user.rows[0].user_id);
res.json(token);
catch (err)
console.log(err.message);
res.status(500).send("Server Error")
);
router.get("/is-verify", authorization, async (res, req) =>
try
res.json(true);
catch (err)
console.log(err.message);
res.status(500).send("Server Error")
)
module.exports = router;
【问题讨论】:
【参考方案1】:您忘记在 auth-middleware 中调用 next
,这将导致请求挂起。通过这样做来修复它:
module.exports = async (req, res, next) =>
try
// ...
req.user = payload.user;
next();
catch (err)
console.log(err.message);
return res.status(403).json("You are not authorized");
【讨论】:
我也试过这个。但是我在我的 try-catch 块之后调用了 next() 但它没有用。现在,我在 try 块的末尾调用了它,它起作用了!太感谢了!我还听说,如果您在函数参数中传递“下一个”参数,它会在异步调用结束时自行调用。显然这不是真的。不过谢谢你指出来。在过去的 3-4 个小时里,我一直在试图弄清楚。以上是关于Postman 卡在“GET”请求中,在使用 PostgreSQL 作为后端的 Node JS 中给出“无法获得响应错误:读取 ECONNRESET”的错误的主要内容,如果未能解决你的问题,请参考以下文章