简体   繁体   中英

NodeJS with Express Framework - relative path to js script

I used Express generator to create node application. Directory structure looks like the following:

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── node_modules
│   └── jquery
│       └── dist
|            |___jquery.min.js
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

In my index.jade file i try to reuse jquery.min.js from node_modules, instead use url to web source:

    doctype html
html
 head
 link(rel='stylesheet', href= '/stylesheets/style_monit.css')

body
 #container
  .row
   .col-sm-4(style='background-color:lavender;') .col-sm-4
   .col-sm-4(style='background-color:lavenderblush;') .col-sm-4     
   .col-sm-4(style='background-color:lavender;') .col-sm-4
   .col-md-4
    textarea#inData.form-control(style='background:#222; color:#00ff00;', rows='8')
 script(type='text/javascript' src='../node_modules/jquery/dist/jquery.min.js')

css file loads great, but in Chrome console i have error that

GET http://localhost:3000/node_modules/jquery/dist/jquery.min.js NOT FOUND

I believe the problem is that the node_modules directory is private and shouldn't be exposed to the client. Only static files in the public directory can be served. See this Stack Overflow answer for more information.

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