douhuan6065 2018-12-09 11:57
浏览 46
已采纳

流明 - 服务于ReactJS项目

I am working on a Lumen / React app for a project. Lumen is obviously supplying the API and React is producing the user interface. I am trying to now serve my React application through Lumen so it can then take over all the user interface work.

I wish to keep the client side section of the app as contained as possible to allow me to switch easily from Lumen to Koa, or any other backend, in the future if I wish. My application structure looks something like this:

enter image description here

As you can see currently the React app is a subfolder of the repo. This is something that I have done with previous Koa projects, however, due to the nature of Nodejs web serves it made it a lot easier to serve these files.

From my testing so far, I think the best way of going about serving the files is to read the contents of the index.html from the client folder, either /build or /public depending on whether production is enabled. I will then need to redirect requests for assets outside the public folder set by the web server to /build or /src in the client folder, again, depending on whether it is in production.

My questions are:

A) Is this possible in a secure and reliable way?

B) Is this the best way of going about serving the React application through Lumen?

  • 写回答

1条回答

  • douren9077 2018-12-09 13:56
    关注

    Probably not the nicest solution, but I just wrote my own scripts to deploy the built version to the Lumen files after the build script is run.

    Development can be done with the React proxy option.

    const copydir = require('copy-dir');
    const fs = require('fs');
    
    copydir.sync('./build', '../public');
    
    fs.readFile('./build/index.html', 'utf8', function (err, data) {
        if (err) throw err;
        fs.writeFile ('../resources/views/index.php', data, function(err) {
            if (err) throw err;
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。