241

I'm using express + node.js and I have a req object, the request in the browser is /account but when I log req.path I get '/' --- not '/account'.

  //auth required or redirect
  app.use('/account', function(req, res, next) {
    console.log(req.path);
    if ( !req.session.user ) {
      res.redirect('/login?ref='+req.path);
    } else {
      next();
    }
  });

req.path is / when it should be /account ??

8
  • 4
    TypeError: Cannot read property 'path' of undefined
    – chovy
    Sep 21, 2012 at 8:24
  • req.route.path is correct and documented here. Which version of express are you using?
    – zemirco
    Sep 21, 2012 at 17:31
  • I'm having the same issue. req.route is undefined. Im using express 3.4.4. What can cause route to be undefined? Nov 12, 2013 at 20:50
  • @vinayr req.route.path still gives me /create instead of /quizzes/create, which is the whole URL Apr 15, 2017 at 20:02
  • This is intended behavior. And you should embrace it. Your handler should not care about the full path, but only about the 'local' part of the path. That's the part after the path it was mounted on. This makes the handler function more easy to reuse in other contexts. Dec 9, 2017 at 18:28

11 Answers 11

331

After having a bit of a play myself, you should use:

console.log(req.originalUrl)

7
  • 4
    I think its got something to do with the positioning of middleware, but you are correct, it doesn't make sense.
    – Menztrual
    Sep 22, 2012 at 5:55
  • 1
    It's definitely the middleware. This happens with additional routers in Express 4 as well. When either is mounted off of a given path, inside of itself it gets to pretend that it's off of the root. That's nice for isolation, but tricky when you don't know how to get what the original full value was. Thanks for posting this!
    – juanpaco
    Apr 23, 2015 at 12:31
  • 3
    Anyone getting here on 4.0, req.url is designed to be mutable by middleware for re-routing purposes, and req.path could be missing mounting points depending upon where it's called. expressjs.com/en/api.html#req.originalUrl Jul 21, 2017 at 19:06
  • 3
    If you don't want the query string included: const path = req.originalUrl.replace(/\?.*$/, '');
    – Adam Reis
    Feb 5, 2019 at 4:03
  • 4
    Warning: This is a misleading answer, based on the OP question. This will also return the query string (e.g. ?a=b&c=5) if it was present. See expressjs.com/en/api.html#req.originalUrl
    – Ciabaros
    May 29, 2019 at 14:32
99

Here is an example expanded from the documentation, which nicely wraps all you need to know about accessing the paths/URLs in all cases with express:

app.use('/admin', function (req, res, next) { // GET 'http://www.example.com/admin/new?a=b'
  console.dir(req.originalUrl) // '/admin/new?a=b' (WARNING: beware query string)
  console.dir(req.baseUrl) // '/admin'
  console.dir(req.path) // '/new'
  console.dir(req.baseUrl + req.path) // '/admin/new' (full path without query string)
  next()
})

Based on: https://expressjs.com/en/api.html#req.originalUrl

Conclusion: As c1moore's answer states, use:

var fullPath = req.baseUrl + req.path;
83

In some cases you should use:

req.path

This gives you the path, instead of the complete requested URL. For example, if you are only interested in which page the user requested and not all kinds of parameters the url:

/myurl.htm?allkinds&ofparameters=true

req.path will give you:

/myurl.html
4
  • 1
    Be careful that if you're doing checks against this URL to also include a check for a trailing slash if it's valid in your app. (i.e. checking for /demo should also potentially check for /demo/).
    – Vinay
    Jun 2, 2016 at 0:20
  • If you don't want the query string included: const path = req.originalUrl.replace(/\?.*$/, '');
    – Adam Reis
    Feb 5, 2019 at 4:03
  • req.path is the shorthand of url.parse(req.url).pathname and this should be the accepted answer
    – sertsedat
    Mar 10, 2020 at 17:00
  • 3
    @sertsedat Incorrect. req.path gives you the relative path to where your app is mounted. If it is mounted at the root than this is correct, but for a path /my/path, within an app mounted at /my, req.url will give /path. May 3, 2020 at 13:32
17

This can produce different results when calling directly in base module i.e. main file (e.g. index.js or app.js) vs calling from inside module via app.use() middleware i.e. route file (e.g. routes/users.js).

API call:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en

We'll be comparing our outputs against above API call


First, we'll see the result from innner module:

  1. We'll be placing our user module inside routes directory, with one route i.e. /profile/:id/:details

    routes/users.js file

    const router = (require('express')).Router();
    
    router.get('/profile/:id/:details', (req, res) => {
    
        console.log(req.protocol);        // http or https
        console.log(req.hostname);        // only hostname without port
        console.log(req.headers.host);    // hostname with port number (if any); same result with req.header('host')
        console.log(req.route.path);      // exact defined route
        console.log(req.baseUrl);         // base path or group prefix
        console.log(req.path);            // relative path except query params
        console.log(req.url);             // relative path with query|search params
        console.log(req.originalUrl);     // baseURL + url
    
        // Full URL
        console.log(`${req.protocol}://${req.header('host')}${req.originalUrl}`);
    
        res.sendStatus(200);
    
    });
    
    module.exports = router;
    
  2. Now we'll import this user module in main module of the application and add /api/users as base path for user module using app.use() middleware

    index.js file

    const app = (require('express'))();
    
    const users = require('./routes/users');
    app.use('/api/users', users);
    
    const server = require('http').createServer(app);
    server.listen(8000, () => console.log('server listening'));
    

Output

[req.protocol] ............. http
[req.hostname] .......... localhost
[req.headers.host] ..... localhost:8000
[req.route.path] .......... /profile/:id/:details
[req.baseUrl] .............. /api/users
[req.path] ................... /profile/123/summary
[req.url] ...................... /profile/123/summary?view=grid&leng=en
[req.originalUrl] .......... /api/users/profile/123/summary?view=grid&leng=en

Full URL:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en


Now, we'll see result if we add routes directly in main module:

  1. We'll define our route right in the main module (i.e. app.js or index.js) and concatenate base path /app/users with route path instead of using middleware. So route will become /api/users/profile/:id/:details

    index.js file

    const app = (require('express'))();
    
    app.get('/api/users/profile/:id/:details', (req, res) => {
    
        console.log(req.protocol);        // http or https
        console.log(req.hostname);        // only hostname without port
        console.log(req.headers.host);    // hostname with port number (if any); same result with req.header('host')
        console.log(req.route.path);      // exact defined route
        console.log(req.baseUrl);         // base path or group prefix
        console.log(req.path);            // relative path except query params
        console.log(req.url);             // relative path with query|search params
        console.log(req.originalUrl);     // baseURL + url
    
        // Full URL
        console.log(`${req.protocol}://${req.header('host')}${req.originalUrl}`);
    
        res.sendStatus(200);
    
    });
    
    const server = require('http').createServer(app);
    server.listen(8000, () => console.log('server listening'));
    

Output

[req.protocol] ............. http
[req.hostname] .......... localhost
[req.headers.host] ..... localhost:8000
[req.route.path] .......... /api/users/profile/:id/:details
[req.baseUrl] ..............
[req.path] ................... /api/users/profile/123/summary
[req.url] ...................... /api/users/profile/123/summary?view=grid&leng=en
[req.originalUrl] .......... /api/users/profile/123/summary?view=grid&leng=en

Full URL:
http://localhost:8000/api/users/profile/123/summary?view=grid&leng=en

We can clearly see in above output that the only difference is of baseUrl which is empty string here. So, the originalUrl also changes & looks same as the url

4
  • 3
    req.route is undefined
    – Jovanni G
    Nov 9, 2021 at 10:00
  • can you share your code snippet (e.g. JSFiddle etc)?
    – TalESid
    Nov 23, 2021 at 6:33
  • The example code is correct, but the results in the answer are not. In the second example, /api/users doesn't appear in the posted output at all, which doesn't even make sense in that context. Apr 14 at 2:09
  • In second example's output, I haven't listed /api/users at all. See the value against [baseUrl] is empty
    – TalESid
    Apr 14 at 5:36
16

UPDATE 8 YEARS LATER:

req.path was already doing exactly same thing that I mentioned here. I don't remember how this answer solved issue and accepted as a correct answer but currently it's not a valid answer. Please ignore this answer. Thanks @mhodges for mentioning this.

Original answer:

If you want to really get only "path" without querystring, you can use url library to parse and get only path part of url.

var url = require('url');

//auth required or redirect
app.use('/account', function(req, res, next) {
    var path = url.parse(req.url).pathname;
    if ( !req.session.user ) {
      res.redirect('/login?ref='+path);
    } else {
      next();
    }
});
4
  • this exactly is what i want. Use it with req.query.ref if login successful
    – Ryan Wu
    Nov 19, 2013 at 16:14
  • This works nicely with universal code since it conforms to Location spec. Less things to remember and easier to unit test across client and server. Oct 11, 2016 at 20:13
  • 1
    req.path is just an alias for url.parse(req.url).pathname
    – mhodges
    Apr 20, 2020 at 22:43
  • 1
    @mhodges Wow, that looks completely true. This is a 8 years old answer and I have no clue how we thought that answer solves the issue. Thanks for mentioning. I'll update answer or maybe just delete it. Oct 25, 2021 at 7:40
13
//auth required or redirect
app.use('/account', function(req, res, next) {
  console.log(req.path);
  if ( !req.session.user ) {
    res.redirect('/login?ref='+req.path);
  } else {
    next();
  }
});

req.path is / when it should be /account ??

The reason for this is that Express subtracts the path your handler function is mounted on, which is '/account' in this case.

Why do they do this?

Because it makes it easier to reuse the handler function. You can make a handler function that does different things for req.path === '/' and req.path === '/goodbye' for example:

function sendGreeting(req, res, next) {
  res.send(req.path == '/goodbye' ? 'Farewell!' : 'Hello there!')
}

Then you can mount it to multiple endpoints:

app.use('/world', sendGreeting)
app.use('/aliens', sendGreeting)

Giving:

/world           ==>  Hello there!
/world/goodbye   ==>  Farewell!
/aliens          ==>  Hello there!
/aliens/goodbye  ==>  Farewell!
1
  • I'm amazed this answer isn't voted up much more, since it clearly answers the question and explains the behaviour (unlike all the higher-voted answers). Thanks! Nov 2, 2022 at 8:28
12

It should be:

req.url

express 3.1.x

1
11

For version 4.x you can now use the req.baseUrl in addition to req.path to get the full path. For example, the OP would now do something like:

//auth required or redirect
app.use('/account', function(req, res, next) {
  console.log(req.baseUrl + req.path);  // => /account

  if(!req.session.user) {
    res.redirect('/login?ref=' + encodeURIComponent(req.baseUrl + req.path));  // => /login?ref=%2Faccount
  } else {
    next();
  }
});
0
6

req.route.path is working for me

var pool = require('../db');

module.exports.get_plants = function(req, res) {
    // to run a query we can acquire a client from the pool,
    // run a query on the client, and then return the client to the pool
    pool.connect(function(err, client, done) {
        if (err) {
            return console.error('error fetching client from pool', err);
        }
        client.query('SELECT * FROM plants', function(err, result) {
            //call `done()` to release the client back to the pool
            done();
            if (err) {
                return console.error('error running query', err);
            }
            console.log('A call to route: %s', req.route.path + '\nRequest type: ' + req.method.toLowerCase());
            res.json(result);
        });
    });
};

after executing I see the following in the console and I get perfect result in my browser.

Express server listening on port 3000 in development mode
A call to route: /plants
Request type: get
1

When using a middleware in express, your request object has several properties you can use to get the correct path:

  • req.baseUrl: /api/account
  • req.originalUrl: /api/account
  • req._parsedUrl.path: /account
  • req._parsedUrl.pathname: /account
  • req._parsedUrl.href: /account
  • req._parsedUrl._raw: /account

PLEASE NOTE: This applies to middlewares

0

For those getting undefined from req.route.path that is correct.

Inside route handler, there's a route. Inside middleware handlers, there's no route.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.