【问题标题】:React application on node express server - how to route节点快递服务器上的反应应用程序 - 如何路由
【发布时间】:2018-02-19 08:06:32
【问题描述】:

我在节点中有简单的快速服务器:

const express = require('express')
const path = require('path')
const application = express()
const port = process.env.PORT || 80

const PUBLIC_DIR = 'public'

application.use(express.static(path.join(__dirname, PUBLIC_DIR)))
application.listen(port)

//handle 404
application.use((req, res) => {
    res.send('404: Page not Found', 404)
});

//handle 500
application.use((error, req, res, next) => {
    res.send('500: Internal Server Error', 500)
});

console.log(['HTTP server running on ', process.env.HOST, ' / ', port].join(''))

当我将“已构建”的反应应用程序放入公共文件夹时,服务器返回 index.html 好。但问题在于反应路由器。

我有这样的路由器:

/
/home
/about

当我转到 url localhost/ 时 - 工作正常,返回带有完整应用程序的 index html,但问题是当我转到 /home、/about 时,服务器返回 404,如何解决?如何重定向到反应路线?希望你能理解我。

感谢您的帮助

【问题讨论】:

    标签: node.js reactjs express


    【解决方案1】:

    构建反应后尝试使用:-

    app.use(require('body-parser').json({ limit: '50mb' }));
    app.use(require('body-parser').urlencoded({ limit: '50mb', extended: true, parameterLimit: 1000000 }));
    app.use(express.static(path.join(__dirname, '../client')));
    
    
    switch (process.env.NODE_ENV) {
        case 'production':
            app.use(express.static('./client/build/'));
            app.use('/', express.static('./client/build/index.html'));
            break;
        default:
            app.use(express.static('./client/'));
            app.use(express.static('./', config.staticOptions));
            app.use('/', express.static('./client/index.html'));
            break;
    }
    

    【讨论】:

    • 我还是 404
    • 我刚刚更新了我的评论,这是我在我的应用程序中所做的,我正在使用 create-react-app aslo
    • 什么是配置?在app.use(express.static('./', config.staticOptions)); ?
    • 它是“staticOptions:{ etag: true, setHeaders: function (res, path, stat) { res.set('Cache-Control', 'public, max-age=31557600') } },"
    【解决方案2】:

    尝试返回 index.html 中的所有路由,如下所示:

    const express = require('express')
    const path = require('path')
    const application = express()
    const port = process.env.PORT || 80
    
    const PUBLIC_DIR = 'public'
    
    application.use(express.static(path.join(__dirname, PUBLIC_DIR)))
    application.listen(port)
    
    app.use(express.static('client/build'));  //use your build path my build path under the root folder is client/build
      const path = require('path');
      app.get('*', (req, res) => {
        res.sendFile(path.resolve(__dirname, 'client','build', 'index.html')); //use your build path my build path under the root folder is client/build
      });
    
    //handle 404
    application.use((req, res) => {
        res.send('404: Page not Found', 404)
    });
    
    //handle 500
    application.use((error, req, res, next) => {
        res.send('500: Internal Server Error', 500)
    });
    
    console.log(['HTTP server running on ', process.env.HOST, ' / ', port].join(''))
    

    【讨论】:

    • 我收到“500:内部服务器错误”
    • 检查你的构建所在的路径目录名称可能不同
    • 我部署的构建位于 /client/build 的根目录下,您可能将它放在与根目录不同的文件夹中
    • 无法在 debian 上运行,始终返回所有文件的 index.html - ico、js 文件等。
    • 更改或添加 app.get 以根据您的要求进行定制。这个想法是,如果您有特定的排除路径,请将其放在本节之前 app.get ('*' 以提供这些文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 2016-02-05
    相关资源
    最近更新 更多