I'm developing a server in NodeJS with Express, but the web structure is a little bit complicate (I didn't do it, and I can't change it either).
The flow is something like this:
- Node server receives a call like
https://localhost/**{id}**. - The ID received is the folder's name where all files (html, js, css,
etc) are stored. By default, it returns
index.html. - File structure of any web doesn't have a strict logic, means that could be more views at the same level the
index.htmlis or in folders, wherever the clients wanted to develop them.
The issue I'm having is how to route the files correctly. Since I'm receiving the ID only when the index it's called, I couldn't figure out how to route links like <a href="view1.html">View 1</a> or even the javascript files calls <script src='scripts/someGreatFunctions.js'></script> since they could also be in the root or in a folder (even the two things at the same time).
My server.js file:
const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();
const config = require('./config');
var webId;
var options = {
key: fs.readFileSync(config.paths.certificate.key),
cert: fs.readFileSync(config.paths.certificate.crt),
requestCert: false,
rejectUnauthorized: false
};
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT");
res.setHeader("Access-Control-Allow-Headers", "Accept, Access-Control-Allow-Headers, Access-Control-Request-Headers, Access-Control-Request-Method, Authorization, Content-Type, Origin, X-Requested-With");
next();
});
app.get('/scripts/:script', function(req, res) {
res.sendFile(req.params.script, {root: config.paths.webs + webId + '/scripts'});
});
app.get('/:script.js', function(req, res) {
res.sendFile(req.params.script + '.js', {root: config.paths.webs});
});
// This routes correctly the index
app.get('/:id', function(req, res) {
webId = req.params.id;
res.sendFile('index.html', {root: config.paths.webs + webId});
});
// This DID NOT work
app.get('/:id/:page', function(req, res) {
//Some code here...
});
https.createServer(options, app).listen(443, function() {
console.log("NodeJS secure server started at port 443");
});