Node.js 调用Axios GET并由express server发送.json()时,在xhr.js上捕获404
在我的客户机Vue.js应用程序上,控制台在调用我的express API时给我一个404错误,该错误带有无效的API标记 例如,呼叫Node.js 调用Axios GET并由express server发送.json()时,在xhr.js上捕获404,node.js,express,xmlhttprequest,axios,http-status-code-404,Node.js,Express,Xmlhttprequest,Axios,Http Status Code 404,在我的客户机Vue.js应用程序上,控制台在调用我的express API时给我一个404错误,该错误带有无效的API标记 例如,呼叫http://localhost:8081/confirmation/invalidToken 给我 xhr.js?b50d:172 GET http://localhost:8081/confirmation/a 404 (Not Found) 当然,有人可能会使用无效链接,因此我希望处理这些错误,而不是在控制台上打印错误 如果在express服务器端,我会发
http://localhost:8081/confirmation/invalidToken
给我
xhr.js?b50d:172 GET http://localhost:8081/confirmation/a 404 (Not Found)
当然,有人可能会使用无效链接,因此我希望处理这些错误,而不是在控制台上打印错误
如果在express服务器端,我会发送响应,如下所示:
return res.status(404);
return res.status(404).send({
message: 'No valid link!',
error,
});
然后控制台错误消失。
编辑:然而,这似乎只是一种情况,因为请求尚未完成,它正在等待。一段时间后,控制台会记录“net::ERR\u CONNECTION\u RESET”错误
只有在我发送如下响应时才会出现:
return res.status(404);
return res.status(404).send({
message: 'No valid link!',
error,
});
或者这个:
return res.status(404).send({
message: 'No valid link!',
error,
});
客户端捕获不适用于此问题
public async sendConfirmation(token: string): Promise<any> {
try {
return axios.get(this.baseURL + '/confirmation/' + token);
} catch (error) {
// tslint:disable-next-line:no-console
console.log(error.response);
}
}
sendConfirmation(this.token)
.then((res) => {
// tslint:disable-next-line:no-console
console.log(res);
if (res.status === 404) {
throw new Error("404");
}
this.data = res.data;
if (res.status === 201) {
this. messagetype = 1;
} else if (res.status === 200) {
this.messagetype = 2;
}
})
.catch((err) => {
this.messagetype = 0;
// tslint:disable-next-line:no-console
// console.log(err );
});
公共异步发送确认(令牌:字符串):承诺{
试一试{
返回axios.get(this.baseURL+'/confirmation/'+token);
}捕获(错误){
//tslint:禁用下一行:无控制台
console.log(error.response);
}
}
sendConfirmation(this.token)
。然后((res)=>{
//tslint:禁用下一行:无控制台
控制台日志(res);
if(res.status==404){
抛出新错误(“404”);
}
this.data=res.data;
if(res.status==201){
this.messagetype=1;
}否则如果(res.status==200){
this.messagetype=2;
}
})
.catch((错误)=>{
this.messagetype=0;
//tslint:禁用下一行:无控制台
//控制台日志(err);
});
有人知道一种捕获控制台错误的方法,而不必删除服务器结果中的自定义消息吗?找到解决方法了吗?