如何将React前端应用安装到Express Server?

时间:2019-04-01 00:21:21

标签: reactjs express sendfile

我使用“ yarn build”构建了前端react应用程序,就像google告诉我要做的那样,我正尝试通过以下方式将“ build”文件夹中的“ index.html”挂载到我的节点快速服务器上res.sendFile。但是,当我实际执行此操作并在chrome broswer上看到网页时,我只看到空白页,除了空白之外,什么都没有。当我打开chrome开发人员工具时,我可以看到...(目录)..这个转发得很好,但是浏览器无法识别出<<标签。如您所知,我只是反应的初学者,这在上周真的很难受。请帮助...

这是有问题的server.js文件...

    const express = require('express');
    const bodyParser = require('body-Parser');
    const path = require('path');
    const httpApp = express();
    const port = 5000;

    httpApp.use(bodyParser.urlencoded({extended: true}));
    httpApp.use(express.static(__dirname + '../client/build/static'));
    httpApp.use(bodyParser.json());

    httpApp.get('*', function(req, res){
        res.sendFile(path.resolve(__dirname, '../client/build', 'index.html'))
    });

    // httpApp.get('/', (req, res)=>{
    //     console.log("Server: get request arrived. Client Home is sent.");
    //     var pathh = path.join(__dirname, '../client/build/index.html');
    //     console.log(pathh);
    //     res.sendFile(path.join(__dirname, '../client/bui-d','index.html'));
    // });

    httpApp.listen(port, (req, res)=>{
        console.log(`server listening on port: ${port}`);
    });

这就是我在chrome浏览器中得到的... enter image description here

0 个答案:

没有答案