简体   繁体   中英

Shared javascript resources between Node.js and Apache/PHP

I am building a website which uses Apache + PHP on port 80 for the frontend, and Node.js + Socket.IO for dynamic updates on port 3000. There are quite a few shared Javascript resources between the two and I'd like to know how I can share them.

Here's my project layout:

project/
    www/                 // Apache DocumentRoot
        index.php
        js/              // Javascript files for the HTML pages
        css/
    app/
        App.php          // All the PHP files
    node/
        node_modules/    // Node modules installed by NPM
        app.js           // Node.js application

When my PHP application generates HTML code, it needs to put some <script> tags in <head> for things like underscore.js, backbone.js, socket.io.js, etcerea. But these scripts are all somewhere in de node_modules directory.

I have seen various examples but most use Node.js to serve the HTML page as well (usually using the express framework). So, they generate script includes like /socket.io/socket.io.js . But in my case I have two servers running on two ports.

So, how can I load the proper Javascript files without manually copying things from the node_modules directory to the js directory under the DocumentRoot?

There are 2 options:

  • Create a symlink (pointing from www/js to node_modules)
  • Create an Apache2 alias in you Apache configuration file (see here )

If you have access to your Apache2 configuration, the 2nd solution is probably the better one. When going for the symlink solution, make sure you double-check the ownership of the folders if you experience problems.

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