nodejs/express include local js file

前端 未结 3 1163
无人共我
无人共我 2020-12-09 04:47

Here is my current folder structure

css
   app.css
js
  app.js
node-modules
index.html
node-server.js
package.json

The node-server is hosti

3条回答
  •  醉话见心
    2020-12-09 05:30

    As Tomasz Kasperek pointed out, you need to let Express know that you intend to host these files in a static directory. This is technically called defining static middleware.

    This should look something like:

    var express = require('express');
    var app = express();
    
    // first parameter is the mount point, second is the location in the file system
    app.use("/public", express.static(__dirname + "/public"));
    

    It's super simple and I suggest you go the route of making some sort of public folder, rather than bothering to make specific files and folders static.

    Then the files would simply be referenced like so from the root index.html:

    
    

    Hope this helps you!

提交回复
热议问题