Hey everyone I was unaware and what to search for this issue, but here is my problem. For our project, we are sharing a server for our applications using express. We are not allowed to share the urls, so I will have to change the url for the sake of the question. Our server root is http://www.server.com, but our project url is http://www.server.com/team01/. How can I serve from /team01/public/css/whatever.css every single time, but keep our files for testing on localhost. I don't want to have to change the links every single time I migrate from localhost to the server. Also for all of our href it is going to server.com which is messing up everything for migrating from localhost to the server. Any help is appreciated. I've used app.use('/public',express.static(path.join(__dirname, '/public'))); in express.
1 Answers
In your HTML files, you can reference:
<link href="/css/whatever.css">
This will tell the browser to use whatever host the web page was loaded from, but to always request the /css/whatever.css
path on that host.
Then, your express server can use this:
app.use(express.static("/team01/public"));
This will tell the express server that when it receives a request for /css/whatever.css
, it should look in /team01/public/css/whatever.css
to see if the file exists and, if so, serve it statically from there.
Using this scheme, you should be able to run on localhost just fine as long as you have a copy of the right files in /team01/public
on your localhost server. Then, when you switch over to the real http://www.server.com
, you just have to make sure you also copy over the /team01/public
files to that host.
FYI, you can also add a <base href="http://server.com/fa17">
to the head of every web page on your site and then that will be used with all relevative URLs in the pages. If you use a template engine for serving your HTML files, then you can control what that path is automatically in that tag via your startup code (based on where your server is running).