Reputation: 856
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.
Upvotes: 0
Views: 212
Reputation: 140
Move the app.get
above app.use
s 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.use
s 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.
Upvotes: 2