/signup与/login路由异常:VS Code报错但Postman请求成功
问题:VS Code提示路由不存在但Postman请求功能正常
- 开发
/api/v1/users/signup和/api/v1/users/login路由时,VS Code持续弹出提示:Error: Can't find /api/v1/users/signup on the server :( !,/login路由出现完全相同的提示 - 但使用Postman发送POST请求到这两个接口时,返回状态均为
success,接口功能完全符合预期
相关代码片段
app.js
app.use("/api/v1/users", userRouter);
usersRoute.js
router.post("/signup", authController.signup); router.post("/login", authController.login);
控制器方法头部定义
exports.signup = catchAsync(async (request, response, next) => {} exports.login = catchAsync(async (request, response, next) => {}
错误排查用代码(含日志打印)
const catchAsync = (asyncFunction) => { console.log("INSIDE CATCHASYNC 1"); return (request, response, next) => { console.log("INSIDE CATCHASYNC 2"); asyncFunction(request, response, next).catch((error) => { console.log(error); console.log(error.stack); console.log("INSIDE CATCHASYNC 3"); next(error); }); }; }; exports.login = catchAsync(async (request, response, next) => { console.log("++++++1+++++++"); const { email, password } = request.body; // 1- 检查邮箱和密码是否提供 if (!email || !password) return next(new AppError(400, "请提供邮箱和密码以登录!")); // 2- 检查用户是否存在,验证密码是否匹配 const user = await User.findOne({ email }) .select("+password"); // 3- 验证通过则生成并返回token if (!user || !(await user.comparePassword(password, user.password))) return next(new AppError(401, "邮箱或密码错误!")); console.log("++++++2+++++++"); const token = signature(user._id); console.log("++++++12+++++++"); response.status(200).json({ status: "success", token }); console.log("above next() in login"); next(); });
控制台运行日志截图

内容的提问来源于stack exchange,提问作者Marya
相关产品推荐
相关产品推荐

