Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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 从Angular到Express服务器的http post请求导致404错误_Node.js_Angular_Express - Fatal编程技术网

Node.js 从Angular到Express服务器的http post请求导致404错误

Node.js 从Angular到Express服务器的http post请求导致404错误,node.js,angular,express,Node.js,Angular,Express,我正在向本地的Azure功能发出请求 url = 'http://localhost:7071/api/saveGraphDataFlow' save(body) { let headers = new HttpHeaders() headers.append('Content-Type', 'application/json') return this.httpClient.post(this.url, body, { headers: headers }).pipe(

我正在向本地的Azure功能发出请求

url = 'http://localhost:7071/api/saveGraphDataFlow'
save(body) {
  let headers = new HttpHeaders()
  headers.append('Content-Type', 'application/json')
  return this.httpClient.post(this.url, body, { headers: headers }).pipe(
     map(res => {
      return res
    })
  )
}
在我的express服务器上,我正在向响应添加COR

const createHandler = require("azure-function-express").createHandler;
const express = require("express");
const routers = require("./routes/routes");
const app = express();
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");

  next();
});
app.use("/api/", routers);

// Binds the express app to an Azure Function handler
module.exports = createHandler(app);
但是,当我发送请求时,会出现以下错误:

Access to XMLHttpRequest at 'http://localhost:7071/api/saveGraphDataFlow' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

飞行前请求未通过

您可以尝试从中删除额外的斜杠

app.use("/api/", routers);
因此它变成:

app.use("/api", routers);
另外,作为旁注,我还没有看到您的API路由器,所以可能有一个额外的斜杠或缺少斜杠在那里。我注意到的另一件事是,您正在为路由器导入一个完整的文件夹,因此请确保您正在立即导入一个文件。我没有看到,所以这可能不是真的

您能否显示./routes/routes imported/required as router。这是查看您的请求是否进入正确路径的唯一方法。您不需要使用cors中间件还是我错了?而且您没有指定任何来源。