I would like to break my node.js/express.js UI (html/resources) out of my existing project directory. IOW, I want to move all of my static html/js/css..etc into an external directory, above my project directory.
Example:
rootpath
/UI_1
Views ( html files)
index.html
public
javascripts
status.js
/NodeJSProject (has express...etc
I am able to serve the html files just fine. However, I am having trouble getting the static resources (js/css..etc) served correctly, or referenced correctly by the html files...
in my node project, in app.js I tired this...
app.use('cust1', express.static(path.join(__dirname, "../UI_1/public")));
and then in the html...
<script src="cust1/javascripts/status.js"></script>
Is this even possible??, thanks .
If /NodeJSProject
is at the same directory level as /UI_1
, then it should work if you add a /
to the path in the script tag and remove /cust1
which isn't anywhere in your static directory hierarchy:
<script src="/javascripts/status.js"></script>
And, change the middleware to this:
app.use(express.static(path.join(__dirname, "../UI_1/public")));
That will make the path that the browser requests absolute rather than relative to where the host HTML page is. When you leave off the leading /
, then the browser uses the path of the containing HTML file which may or may not be what you want. When you use a /
on it, then nothing is relative to the path of the containing HTML file and it's a lot easier to be consistent about what will be requested so you can match it up with express.static()
.
I have no idea why "cust1" was in your app.use()
statement because you show no cust1
anywhere in the static resources so I recommended you remove it. If you think that needs to be there, then please explain what it is for and why?
You could also have cust1
in both paths/URLs with the leading /
:
<script src="/cust1/javascripts/status.js"></script>
and
app.use(express.static("/cust1", path.join(__dirname, "../UI_1/public")));
But, it does not seem to be required. If there's a cust2
, then you'd have to customize everything for each customer and have separate middleware for each customer.
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.