Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 661
user2452057 Avatar asked Jul 07 '14 15:07

user2452057


People also ask

What is req originalUrl?

The req.originalUrl property is much like req.url however, it retains the original request URL thereby allowing you to rewrite req.url freely for internal routing purposes.

What is req body in ExpressJS?

The req. body object allows you to access data in a string or JSON object from the client side. You generally use the req. body object to receive data through POST and PUT requests in the Express server.

What is Express Urlencoded?

urlencoded() is a built-in middleware in Express. js. The main objective of this method is to parse the incoming request with urlencoded payloads and is based upon the body-parser. This method returns the middleware that parses all the urlencoded bodies.

What is req query in Express?

query is a request object that is populated by request query strings that are found in a URL. These query strings are in key-value form. They start after the question mark in any URL.


2 Answers

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.

like image 192
Mike Avatar answered Oct 30 '22 09:10

Mike


The problem is that there are actually two entirely separate requests coming in to your server. The request for the /login page doesn't know anything about the page that the client requested before that. So when you redirect to the /login page, you need to save information about the originally-requested URL somewhere. I know of 2 solutions:

  1. Some fancy work with session states allowing you to save the original URL as a variable, probably using promises.

  2. Add the original URL as a query parameter to the /login request.

Check out this answer: How do I find original request path before redirect Express 4

like image 24
dwhieb Avatar answered Oct 30 '22 09:10

dwhieb