简体   繁体   中英

Load same file in front-end and back-end

I have a web server (back-end) written using node. It will communicate with the front-end written in react. I would like to 'require' a file for a data structure and also use that same file in a script in the front-end. I can seen how this can easily be done using json - However, I would like to see if this can be done using plain javascript.

There are multiple ways to do this, but here is a simple one, using a monorepo structure.

You file structure will look like this:

/
 packages
    backend
       package.json
       index.js
    frontend
       package.json
       index.js
    common
       index.js
       package.json

Then, in each of the frontend and backend package.jsons, you can include common package as a dependency using the file:.. syntax

ie.

    dependencies: {
         "common" : "file:../common"
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM