简体   繁体   中英

Routing on NodeJS Express

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:

  1. Node server receives a call like https://localhost/**{id}** .
  2. The ID received is the folder's name where all files (html, js, css, etc) are stored. By default, it returns index.html .
  3. File structure of any web doesn't have a strict logic, means that could be more views at the same level the index.html is 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");
});

I am also in a learning phase. Hope this will help.

app.get('/test/:id/:page', function(req, res, next) {
    let id = req.params.id;
    let page = req.params.page;
    console.log('The id: ' + id);
    console.log('The page: ' + page);
});

I finally got the answer. Seems like it's very important the order in which gets are declared. Also, I used a regular expression to make it a little more generic.

/**
 * HTTP GET for files (.html, .js, etc.) from another folder level
 */
app.get('/:id/:folder/:file', function(req, res) {    
  if (typeof(webId) === undefined) {
    webId = req.params.id; 
  }
  let folder = req.params.folder;
  let file = req.params.file;

  res.sendFile(file, {root: config.paths.webs + webId + '/' + folder});
});

/**
 * HTTP GET for .js files from the base path
 */
app.get(/\/\w+.\b(js)/, function(req, res) {
  let lastBar = req.url.lastIndexOf('/');
  let script = req.url.substr(lastBar);

  res.sendFile(script, {root: config.paths.webs});
});

/**
 * HTTP GET for index page
 */
app.get('/:id', function(req, res) {
  webId = req.params.id;
  res.sendFile('index.html', {root: config.paths.webs + webId});
});

/**
 * HTTP GET for view from base path
 */
app.get('/:id/:page', function(req, res) {
  if (typeof(webId) === undefined) {
    webId = req.params.id; 
  }
  let page = req.params.page;

  res.sendFile(page, {root: config.paths.webs + webId});
});

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