【问题标题】:Node Express serving is serving build/ even without correct headersNode Express 服务正在服务构建/即使没有正确的标头
【发布时间】:2019-09-21 19:08:26
【问题描述】:

所以我开始使用 create-react-app 并在我的机器上创建了一个 react-app。我能够在本地主机上运行它并查看网页。接下来,我运行npm run build 并让它构建./build 目录。

然后我创建了一个名为 server.js 的文件,其内容如下:

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

const auth_key = require('./correct_auth.json').auth;

const app = express();
app.disable('x-powered-by');
app.use(express.static(path.join(__dirname, 'build')));

app.get(
    '*',
    (req, res) => {
        const headers = req.headers;
        if (headers[`auth-token`] && headers[`auth-token`] === auth_key) {
            console.log(`auth-token: ${headers[`auth-token`]}`);
            res.sendFile(path.join(__dirname, `build`, `index.html`));
        } else {
            res.sendStatus(404);
        }
    }
);

app.listen(
    process.env.PORT || 5000,
    () => {
        console.log(`Frontend start on localhost:5000.`);
    }
);

我的目标是仅在一个人拥有正确的 auth-token 标头时才提供网页。但是,在没有正确标题或什至没有指定标题的情况下访问该站点,仍会提供主页。

我应该注意到,我在 DigitalOcean 的 Ubuntu 服务器上运行它,并使用 Nginx 为 localhost:5000 提供服务。 IE,我可以访问 api.XXXXX.com 访问该站点,无论我有什么标题,我总是得到该站点的服务。

我的目录结构如下:

build
correct_auth.json
node_modules
package.json
public
server.js
src

【问题讨论】:

    标签: node.js express authentication


    【解决方案1】:

    您的路线:

    app.use(express.static(path.join(__dirname, 'build')));
    

    在您检查标题之前,以便在不检查标题的情况下提供文件。

    我建议您先制作一个中间件来检查标头,如果它们不存在,则返回适当的错误状态。

    例如,把这个放在第一位:

    app.use((req, res, next) => {
        const headers = req.headers;
        if (headers[`auth-token`] && headers[`auth-token`] === auth_key) {
            console.log(`auth-token: ${headers[`auth-token`]}`);
            // continue on to other routes
            next();
        } else {
            // 401 status is for not-authorized
            res.sendStatus(401);
        }
    });
    

    【讨论】:

    • 非常感谢您的建议。根本不精通express/restify!我试试看
    • @SomeGuy - 我添加了一个示例。
    【解决方案2】:

    以下代码运行正常,我在 cmets 中提到了原因:

    const express = require('express');
    const bodyParser = require('body-parser');
    const path = require('path');
    
    const auth_key = require('./correct_auth.json').auth;
    
    const app = express();
    app.disable('x-powered-by');
    
    // Put the header check middleware first, So all request mentioned after this code will go through this.
    app.get(
        '*',
        (req, res) => {
            const headers = req.headers;
            if (headers[`auth-token`] && headers[`auth-token`] === auth_key) {
                console.log(`auth-token: ${headers[`auth-token`]}`);
                res.sendFile(path.join(__dirname, `build`, `index.html`));
            } else {
                res.status(404).send("Authentication failed");
            }
        }
    );
    
    // This should come after the middleware.
    app.use(express.static(path.join(__dirname, 'build')));
    
    
    app.listen(
        process.env.PORT || 5000,
        () => {
            console.log(`Frontend start on localhost:5000.`);
        }
    );
    

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2019-02-26
      • 2013-01-09
      • 2014-01-23
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多