Difference between req.url and req.originalUrl in Express.js version 4

express, node.js

Solution

From the Express.js documentation:

`req.url` is not a native Express property, it is inherited from Node’s `http` module.

This property is much like `req.url`; however, it retains the original request URL, allowing you to rewrite `req.url` freely for internal routing purposes. For example, the “mounting” feature of `app.use()` will rewrite `req.url` to strip the mount point.

In other words, `req.url` might be overwritten during the internal routing, while `req.originalUrl` will remain untouched.

Problem

I am trying to implement login feature in Express.js version 4 app. I need to determine whether an user is logged in before he can do certain actions. So I have a middleware called as `isLoggedIn` which solely checks if user object is in session, if not, the user is redirected to login page. After the user successfully logs in, he must be redirected to the original URL. ``` app.get('/someaction', isLoggedIn, 'actual function to be executed'); ``` Now inside `isLoggedIn` method, I see that `req.url` and `req.originalUrl` contains the requested action. However if the user is not logged in, when redirected to `/login` page, both `req.url` and `req.originalUrl` has `/login` as the contents. From what I read here, I can override req.url for internal routing purposes. But in my case both `req.url` and `req.originalUrl` gets overridden by `/login` action. What am I doing wrong?

Original source