4

In my public folder I have the index.html file and my route handler is like this

 router.get('/', function (req, res, next) {
   // res.send('index.html');
    if (req.user)
        res.redirect('home');
    else
        res.redirect('login');
 });

As you can see , I have commented out the serving of index.html file , but nodejs still serves the index.html from the public directory instead of redirecting to home or login. But if I remove/rename the index.html file then it works fine.

So How can I configure nodejs so that it invokes the route handler , not the serve index file on request ?

2 Answers 2

6

The express.static() middleware includes an index option that allows you to change the default file name.

Or, for your intentions, to disable the feature entirely:

app.use(express.static(path.join(__dirname, 'public'), {
    index: false
}));
Sign up to request clarification or add additional context in comments.

Comments

4

This is due to ordering of the app.use , previously it was

app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);

changing this to

app.use('/', index);
app.use(express.static(path.join(__dirname, 'public')));

Solves the problem.

1 Comment

yeah i guess this is more like it, index: false on root, did not seem to work for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.