zbeyens
zbeyens

Reputation: 321

Expressjs - How to GET '/' route for every request?

Building an app with ExpressJs, users can now request a user profile with localhost:5000/user/12345.

Then, the server process it like this:

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'index.html'));
});

Problem is that Express takes the (wrong) '/user' route for all the files to send:

GET /user/client/css/fonts/font-awesome.min.css 304 1076.704 ms - -

How can I keep the '/' route ? Like this:

GET /client/css/fonts/font-awesome.min.css 304 1076.704 ms - -

Upvotes: 0

Views: 68

Answers (2)

Stefan Idriceanu
Stefan Idriceanu

Reputation: 178

In order to request a specific user profile you must send the id of the user along the specific route. If you use * all your requests for resources will be responded by nodejs with that specific end-point that you have written.

Instead of what you use now, use :

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

    // access the id with req.params.id;
    res.sendFile(path.join(__dirname, 'index.html'));
});

Upvotes: 1

use this before defining your routes

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

Upvotes: 1

Related Questions