Replies: 2 comments
-
You can do this with the following example code: const express = require('express');
const app = express();
app.use((req, res, next) => {
if(req.hostname === 'example.com') {
express.static('./example.com')(req, res, next);
return;
}
if(req.hostname === 'example.net') {
express.static('./example.net')(req, res, next);
return;
}
res.sendStatus(404);
});
app.listen(3000); For hosting static files, it is better to use a reverse proxy in production. The express team also recommends this. |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can optimize your code using the Here's the code with the middleware: const express = require('express');
const app = express();
app.use((req, res, next) => {
let root = '';
if (req.hostname === 'example.com') {
root = './example.com';
} else if (req.hostname === 'example.net') {
root = './example.net';
} else {
return res.sendStatus(404);
}
express.static(root)(req, res, next);
});
app.listen(3000); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I made my own Router and i would like to serve files.
For example here is my folder nomenclature:
./i.domain.com/resources/file.png
./bin.domain.com/resources/otherFile.png
How can i serve static files depending of the domain (So One folder per domain)
IF (HOST=== "i.domain.com") THEN SERVE i.domain.com/resources/
ELSE IF (HOST=== "bin.domain.com") THEN SERVE bin.domain.com/resources/
Beta Was this translation helpful? Give feedback.
All reactions