Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 角度前端未从express后端获取http请求的适当响应_Node.js_Angular_Express - Fatal编程技术网

Node.js 角度前端未从express后端获取http请求的适当响应

Node.js 角度前端未从express后端获取http请求的适当响应,node.js,angular,express,Node.js,Angular,Express,我使用Angular 7向Express4后端代码发送http请求,但不断得到404响应。我认为这可能与我列出http请求路径的方式有关,但不确定。这是我第一次尝试这样的事情。我已经在Angular中设置了一个proxy.conf.js文件,以允许跨源通信(Angular在localhost:4200上运行,express在localhost:8085上运行)。我的快递代码保存在C:/ABC/myapp下。以下是相关代码: Angular proxy.conf.js文件: { "/": {

我使用Angular 7向Express4后端代码发送http请求,但不断得到404响应。我认为这可能与我列出http请求路径的方式有关,但不确定。这是我第一次尝试这样的事情。我已经在Angular中设置了一个proxy.conf.js文件,以允许跨源通信(Angular在localhost:4200上运行,express在localhost:8085上运行)。我的快递代码保存在C:/ABC/myapp下。以下是相关代码:

Angular proxy.conf.js文件:

{
  "/": {
    "target": "http://localhost:8085",
    "secure": false,
    "logLevel": "debug"
  }
}
发送http请求的角度服务代码:

export class ContactUsService {
private url = '/contactus';
private result: any;
message: string;

addMessage(contactUsMessage: contactUsInterface): Observable<contactUsInterface> {
  return this.http.post<contactUsInterface>(this.url, contactUsMessage). (retry(3));
};

constructor(private http: HttpClient) {}
};
contactUs.js代码(用于contactUsRouter):

当我到达contactus页面(url:localhost:4200/contactus)并执行表单的submit按钮时,我收到以下错误: 在浏览器控制台中:“HTTP404:未找到-服务器未找到任何与请求的URI(统一资源标识符)匹配的内容。 (XHR)张贴—

在npm日志中:“POST/contactus 404 3.081 ms-2128 错误:无法在视图目录“C:\ABC\myapp\views”中查找视图“Error”


关于我做得不对有什么智慧的话吗?

目前,您正在公开一条
POST/contactus/contactus
的路由,因为您正在使用语句
app.use('/contactus',contactUsRouter)指定基本路由;
,然后添加/附加一个附加的/额外的
/contactus
,并注册POST/PUT路由

尝试将POST路径更改为just
“/”

var express = require('express');
var router = express.Router();
var contactUsMessage = require('../models/contactUsMessage');

  router.route('/')
    .get(function(req,res,next){
      res.send("Hello")
    })
    .post(function(req,res,next){
      res.send("Hello")
    });

module.exports = router;

希望这会有所帮助!

目前,您正在公开一条
POST/contactus/contactus
路由,因为您正在使用语句
app.use('/contactus',contactUsRouter);
指定一条基本路由,然后添加/附加一条额外的
/contactus
,并注册POST/PUT路由

尝试将POST路径更改为just
“/”

var express = require('express');
var router = express.Router();
var contactUsMessage = require('../models/contactUsMessage');

  router.route('/')
    .get(function(req,res,next){
      res.send("Hello")
    })
    .post(function(req,res,next){
      res.send("Hello")
    });

module.exports = router;

希望这能有所帮助!

您的代理配置文件工作不正常

请求仍在尝试在运行在端口4200((XHR)POST-)上的angular应用程序中查找POST方法路由/contactus,该端口不存在,因此您将获得404

确保您已遵循官方网站上提到的步骤-

建议您使用代理路径/api而不是/

{
  "/api": {
    "target": "http://localhost:8085",
    "secure": false
  }
}

希望对您有所帮助

您的代理配置文件工作不正常

请求仍在尝试在运行在端口4200((XHR)POST-)上的angular应用程序中查找POST方法路由/contactus,该端口不存在,因此您将获得404

确保您已遵循官方网站上提到的步骤-

建议您使用代理路径/api而不是/

{
  "/api": {
    "target": "http://localhost:8085",
    "secure": false
  }
}

希望对您有所帮助

您似乎还没有在
contactUs.js
中定义POST方法,您是否可以使用Postman之类的Rest工具访问Api?目前您在
/contactUs/contactUs
中公开了一个POST路由。这是因为
/contactUs
的基本路由是通过
应用程序指定的('/contactus',contactus路由器)
。然后您再次使用
/contactus
定义POST。您似乎还没有在
contactus.js
中定义POST方法。您是否能够使用Postman之类的Rest工具访问Api?目前您在
/contactus/contactus
中公开了一个POST路由。这是因为
/contactus
的基本路由是sp通过
app指定。使用('/contactus',contactUsRouter);
。然后您再次使用
/contactus
定义POST。哎呀,我的意思是写“POST”而不是“put”。这似乎至少解决了我询问的错误。现在我在npm日志中得到这个错误:“get/contactus 500”。浏览器控制台给出了这个错误:HTTP500:服务器错误-服务器遇到意外情况,无法完成请求。GET-”。此外,即使angular应用程序未发出GET请求,router.route是否也会对GET请求进行操作?节点日志显示:“[HPM]GET/contactus-”>“当我第一次检索website.com/contactus.navigation到website.com/contactus时,即使没有任何
HttpClient.get()
,将触发express
GET/contactus
触发。如另一个答案中所述,可能建议将API路由放在某种基本/前缀下,例如
/API/
,以避免产生类似的冲突。您能详细说明这一点吗?使用/API/如何避免此类冲突?如果要这样做,我会这样做吗必须更改angular app中的所有路径以包含“/api/”以便传递到http请求的url类似于“/api/contactus”?是的,您将更新express应用程序中的所有REST API路由以使用/API前缀,并更新Angular中的所有请求,以便对任何HttpClient请求也使用/API前缀。通常,您将向express添加捕获所有逻辑以加载(
sendFile()
)你的Angular的index.html任何时候都没有API端点未命中/匹配。谢谢。是关于“/API”的什么避免了这些类型的冲突?我假设sendFile()的这个“包罗万象”逻辑将在app.js的错误处理程序中,还是我必须在express应用程序的每个路由器代码中都包含它?哎呀,我想写“post”而不是“put”。这似乎至少解决了我询问的错误。现在我在npm日志中得到了这个错误:“get/contactus 500”。浏览器控制台给出了这个错误:“HTTP500:SERVER error-服务器遇到了意外情况,阻止了它完成请求。get-“。此外,即使angular应用程序没有发出get请求,router.route是否也会对get请求进行操作?节点lo