【问题标题】:Node Express proxy getting Status Code: 404 Not Found instead of 200 success codeNode Express 代理获取状态代码:404 Not Found 而不是 200 成功代码
【发布时间】:2020-01-23 01:00:54
【问题描述】:

我的 server.js 文件如下所示:

const express = require('express');
const http = require('http');
const path = require('path');
//const request = require('request');
const app = express();
var cors = require('cors')
app.use(cors())

app.all('*', 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-credentials", "true"),
        // res.header("access-control-allow-methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS"), 
        res.header("access-control-allow-methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS"),

        next();
});

var corsOptions = {
    origin: 'http://localhost:8080',
    optionsSuccessStatus: 200
}

const port = process.env.PORT || 3001;

app.use(express.static(__dirname + '/dist/myApp'));

app.get("/*", (req, res) => res.sendFile(path.join(__dirname)));

app.post("/dashboard", cors(corsOptions), function(req, res) {
    console.log(req.headers); // the posted data
    console.log(req.query.module);
    res.sendStatus(200)
});

const server = http.createServer(app);

server.listen(port, () => console.log('Running......'));

当我执行 node server.js 并在浏览器中点击 http://localhost:3001/ 时,我得到了我在同一个文件中提到的 /dashboard 的页面。

但我收到状态代码:404 Not Found。 网络选项卡中显示的 URL 是 http://localhost:3001/student/allstudent

我还应该改变什么?如果从 4200 端口的 proxy.conf.json 开始,它将正常工作。但现在我没有从 proxy.conf.json 开始,因为我正在启动节点 server.js。可以帮助我。我们是否应该为 express 提供任何代理与 Angular 节点服务器不同。

【问题讨论】:

  • 我认为,您的快速服务器中没有学生/所有学生的路线。请先做,然后再试一次。
  • 当我在 4200 上启动节点服务器以获取角度和使用代理的 8080 后端时,我正在使用该应用程序。但现在我只想使用快递通过请求标头获取我的组织单点登录用户名。从那里获取 app.js 文件中的用户名后,我想研究常规角度应用程序如何处理所有路由和所有内容。只是为了从请求中获取该用户名并继续使用 Angular,因为它是现在的工作方式。为此,我们应该在 app.js 中进行哪些更改,除此之外,我们还应该进行任何其他更改。
  • 能否请您告诉我是否必须使用 like route.get("/*", (req, res) => res.sendFile(path.join(__dirname)));而不是 app.get("/*", (req, res) => res.sendFile(path.join(__dirname)));在所有地方或。我正在获取我的主页,当我单击某个按钮时,它会点击正确的 url,但它会给出 404。我是否还需要为 node server.js 加注星标 npm start?还是只有 node server.js 就足够了?因为如果离开 express 而只是 npm 开始使用 rest api 正常工作。现在问题仅用于快速服务器

标签: node.js angular express


【解决方案1】:

?这是我的文件夹结构示例:

/**
 * dist
 *   - myApp
 *     - index.html
 * src
 *   index.js
 * package.json
 */

在我的index.js 中,我在上面使用您的代码,但在您的app.get('/*') 中,我使用下面的代码进行更改?:

app.get("/*", (req, res) => res.sendFile(path.join(__dirname, '../dist/myApp', 'index.html')));

使用上面的代码?,它工作正常。

更新:为什么会出现错误?

?发送POST请求时出现404错误的原因是因为您的快递服务器中没有任何POST方法。

??‍?所以,例如,你可以添加与上面相同的路由,不要忘记将方法从GET更改为POST

POST方法相同的Route示例

app.post("/*", (req, res) => res.sendFile(path.join(__dirname, '../dist/myApp', 'index.html')));

所以,现在你有 1 条路线与 2 条方法相同,即GETPOST,如下面的代码:

// route with GET METHOD
app.get("/*", (req, res) => res.sendFile(path.join(__dirname, '../dist/myApp', 'index.html')));
// route with POST METHOD
app.post("/*", (req, res) => res.sendFile(path.join(__dirname, '../dist/myApp', 'index.html')));

现在,您可以再试一次,如果它与POST 方法一起使用并且您不需要GET 方法,那么您可以使用GET 方法删除那些.

希望对你有帮助。

【讨论】:

  • 当我在浏览器中输入localhost:3001 时,它会转到localhost:4200/dashboard,这很好,因为我的角度路由是这样定义的。现在我在仪表板页面上有链接,当我单击该链接时,我有像 this.http.post("/api/student/allstudent", jsonObj, headerObj) 这样的 API 调用。角度代理如下“/api/*”:{“target”:“localhost:8080”,“secure”:false,“changeOrigin”:true,}。实际上,当我执行服务 --proxy-config proxy.conf.json 时,它工作正常。但是当我做 node server.js 时我想工作一样,因为我想使用 express 服务器
  • 我收到类似以下状态 404 的错误,statusText:“未找到”,url:“localhost:3001/api/student/allstudent”无法发布 /api/student/allstudent。如何使用与常规角度相同的 express。只是我添加了 server.js 文件并从节点 server.js 开始。只有我做了这个改变。如果我 ng serve --proxy-config proxy.conf.json 它将完美运行。问题是当我从 node server.js 开始时请帮助我,因为我对 angular 和 UI 完全陌生
  • 非常感谢您的 cmets。添加代理后,它工作正常。当我在页面加载后单击某个链接时,我能够看到我的请求标头。但是我想在页面加载本身时获取请求标头。从组织登录页面单点登录后,首次页面将加载。所以我想在页面加载时获取标题,以便我可以获取我的用户 ID,并基于此我可以根据条件路由到不同的页面。有什么想法请帮忙
猜你喜欢
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
相关资源
最近更新 更多