从 Angular 访问 Node.js API 时连接被拒绝
Connection refused when accessing Node.js API from Angular
我是 Angular 的新手,我正尝试在我的 Angular 网页中实施用户身份验证。作为后端,我使用 Node.js Express 框架,我使用护照进行身份验证。
当我从浏览器访问服务器 URL 时,它工作正常,但是当我使用相同的 URL 从 Angular 调用 API 时,出现连接被拒绝错误404.
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (users, line 0)
这是我的 URL,可以在浏览器中使用,但不能在 Angular 中使用(我使用 https):
https://192.168.1.103:8000/users
这是我的 Angular API 的样子:
get_user() {
return this.http.post(environment.apiUrl + 'users', {
observe: 'body',
withCredentials: true,
headers: new HttpHeaders().append('Content-Type', 'application/json')
});
}
我的 API 在节点服务器上看起来像:
router.get('/users', isValidUser, function (req, res, next) {
return res.status(200).json(req.user)
});
function isValidUser (req, res, next) {
if(req.isAuthenticated()) next ();
else
return res.status(401).json({message: 'Unauthorized request'});
}
浏览器控制台的输出如下所示:
[Error] ERROR
HttpErrorResponse
error: "<!DOCTYPE html><html><head><title></title><link rel=\"stylesheet\" href=\"/stylesheets/style.css\"></head><body><h1>No…"
headers: HttpHeaders {normalizedNames: Map, lazyUpdate: null, lazyInit: function}
message: "Http failure response for https://192.168.1.103:8000/users: 404 Not Found"
name: "HttpErrorResponse"
ok: false
status: 404
statusText: "Not Found"
url: "https://192.168.1.103:8000/users"
Prototyp HttpErrorResponse
defaultErrorLogger (vendor.js:43427)
handleError (vendor.js:43479)
next (vendor.js:74051)
(anonymná funkcia) (vendor.js:69627)
__tryOrUnsub (vendor.js:115680)
next (vendor.js:115619)
_next (vendor.js:115567)
next (vendor.js:115544)
next (vendor.js:115330)
emit (vendor.js:69589)
run (polyfills.js:3359)
onHandleError (vendor.js:73335)
runTask (polyfills.js:3406)
invokeTask (polyfills.js:3700)
timer (polyfills.js:5885)
您在 angular 代码中使用 post 请求而不是 get 请求。我假设你没有在 nodejs 中定义 /users
的 post 路由,因此错误
尝试
get_user() {
return this.http.post(environment.apiUrl + 'users', {
observe: 'body',
withCredentials: true
});
}
您也不需要设置 Content-Type header 因为它是由 angular HttpClient
自动设置的
我是 Angular 的新手,我正尝试在我的 Angular 网页中实施用户身份验证。作为后端,我使用 Node.js Express 框架,我使用护照进行身份验证。
当我从浏览器访问服务器 URL 时,它工作正常,但是当我使用相同的 URL 从 Angular 调用 API 时,出现连接被拒绝错误404.
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (users, line 0)
这是我的 URL,可以在浏览器中使用,但不能在 Angular 中使用(我使用 https): https://192.168.1.103:8000/users
这是我的 Angular API 的样子:
get_user() {
return this.http.post(environment.apiUrl + 'users', {
observe: 'body',
withCredentials: true,
headers: new HttpHeaders().append('Content-Type', 'application/json')
});
}
我的 API 在节点服务器上看起来像:
router.get('/users', isValidUser, function (req, res, next) {
return res.status(200).json(req.user)
});
function isValidUser (req, res, next) {
if(req.isAuthenticated()) next ();
else
return res.status(401).json({message: 'Unauthorized request'});
}
浏览器控制台的输出如下所示:
[Error] ERROR
HttpErrorResponse
error: "<!DOCTYPE html><html><head><title></title><link rel=\"stylesheet\" href=\"/stylesheets/style.css\"></head><body><h1>No…"
headers: HttpHeaders {normalizedNames: Map, lazyUpdate: null, lazyInit: function}
message: "Http failure response for https://192.168.1.103:8000/users: 404 Not Found"
name: "HttpErrorResponse"
ok: false
status: 404
statusText: "Not Found"
url: "https://192.168.1.103:8000/users"
Prototyp HttpErrorResponse
defaultErrorLogger (vendor.js:43427)
handleError (vendor.js:43479)
next (vendor.js:74051)
(anonymná funkcia) (vendor.js:69627)
__tryOrUnsub (vendor.js:115680)
next (vendor.js:115619)
_next (vendor.js:115567)
next (vendor.js:115544)
next (vendor.js:115330)
emit (vendor.js:69589)
run (polyfills.js:3359)
onHandleError (vendor.js:73335)
runTask (polyfills.js:3406)
invokeTask (polyfills.js:3700)
timer (polyfills.js:5885)
您在 angular 代码中使用 post 请求而不是 get 请求。我假设你没有在 nodejs 中定义 /users
的 post 路由,因此错误
尝试
get_user() {
return this.http.post(environment.apiUrl + 'users', {
observe: 'body',
withCredentials: true
});
}
您也不需要设置 Content-Type header 因为它是由 angular HttpClient
自动设置的