Static routing in node express

Presidenten

Im using a set of static routes in Node Express and are experiencing a very strange phenomenon.

The routing is set up so that '/list/*' and '/setup/*' gets to different html-files where the directories are used kind of as storage id, for example updating info on the page url/setup/12345 would store info in 12345.

To be able to load scripts and such there is also route with regex matching /assets/ to allow url/setup/assets/script.js to be reached without routing to setup.html with a new storage id.

My problem is that this works for url/setup/assets/script.js but not for url/list/assets/script.js even though they have identical routings.


Edit:

Navigating to url/list/assets/script.js leads to list.html (unwanted behaviour)

Navigating to url/setup/assets/script.js leads to script.js (wanted behaviour)


Any ideas on why '/list/*'` wont work?

Here are my static routes:

app.use(/assets/, express.static(wwwPath));

app.use('/list/*', function(req, res, next) {
    res.sendFile('list.html', { root: wwwPath });
});

app.use('/setup/*', function(req, res, next) {
    res.sendFile('setup.html', { root: wwwPath });
});
Presidenten

The solution was to use custom middleware. Here are the new routes:

var requestParser = function(req, res, next) {
    if(req.originalUrl.indexOf('/assets/') >= 0) {
        var assetPath = path.join(wwwPath,  req.path.slice(req.url.indexOf('/assets/')));
        fs.stat(assetPath, function(error, stat){
            if(stat && stat.isFile()) {
                res.sendFile(assetPath);
            }
            else{ 
                res.status(404).send('<h1>404</h1>');
            }
        });
    }
    else {
        next();
    }
};

app.use(requestParser);

app.use('/list/*', function(req, res, next) {
    res.sendFile('schema.html', { root: wwwPath });
});

app.use('/setup/*', function(req, res, next) {
    res.sendFile('setup.html', { root: wwwPath });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Problem with static routing in Node.js using express

From Dev

Node JS/Express Routing

From Dev

Express + node - routing 101

From Dev

Node.js with express routing and sub routing

From Dev

Node/Express angular 5 routing

From Dev

Routing problems with Node(Express).js

From Dev

Serve static files with Node/Express

From Dev

Express middleware for express.static() not routing correctly and throwing next() is not a function

From Dev

Node js (Express ) express.static is not working

From Dev

node express routing pass variable to required module

From Dev

Routing for Posts and Comments with Node/Express not working

From Dev

How to modularize routing with Node.js Express

From Dev

node.JS Express passport routing

From Dev

Conditional routing in controller not working Node Express

From Dev

Express Node.js Routing Issue

From Dev

Express routing static file: Failed to load module script

From Dev

`express.static()` keeps routing my files from the route

From Dev

Make Express Static stop "pending" with Node 14?

From Dev

Static file, stylesheet with Node.js and Express

From Dev

node express.static to read all directory

From Dev

Conditionally serve React static files with Node (express.static)

From Dev

Routing in express

From Dev

socket.io is not working with static file routing node.js

From Dev

How routing in Express.js/Node.js web app?

From Dev

Express over Node.js routing - Cannot Get /

From Dev

Node express routing sendfile fails depending on file location

From Dev

Node.js / Express routing doesn't work properly

From Dev

Node.js, Express not routing parameterized GET request

From Dev

Can't Serve Static HTML File with Express / Node

Related Related

HotTag

Archive