0
votes

I have a problem with my node app. I use express to server pages with my node app. But it looks like that the base function:

app.use(express.static('public'));
app.use(express.static('allure-report'));

app.get('/', (req, res) => {
    //myHtmlCode is read my fs from index.html
    //res.send(myHtmlCode);
    //res.render('index', { html: myHtmlCode })
    res.writeHead(200, {
        'Content-Type': 'text/plain'
    });
    res.write(myHtmlCode);
});

In all this cases, node will server the index.html page that is inside the public folder. If I remove the index.html page from public folder, it will server the index.html from allure-report folder.

How I can tell node not to serve the index.html file and server the dynamic content in the "app.get('/', (req, res) => {" function? It do not work with send, render or write.

It looks like that node complete ignore the function or route "/" if the index.html file is available.

It looks like that

app.get('/', function(req, res) {

is never called.

1
By reordering the app.use and app.get calls. The order you write them in the code is the priority it get matched.Ricky Mo

1 Answers

2
votes

Move the app.get above app.uses and call next in the request handler like so:

app.get('/', function(req, res, next) { ...; next(); })    
app.use(express.static('public'));    
app.use(express.static('allure-report'));

Express handles responses in the order that you define all app.uses so express.static('public') or express.static('allure-report') will handle the response first and will not give a chance for your function to run.

When you call next() in your response handler it will pass the request to the next handler in line.