How to get Request Origin in Express.js?

This article will show you how to get a request Origin and request host in express.js.

Related Articles:

1. What is Request Origin?

The request origin indicates the domain originating the request and where a fetch to an API originates.

The Origin header includes three parts: scheme (protocol), hostname (domain), and port :

  Origin: <scheme>://<hostname>:<port>

Scheme: is the protocol that is used. It can be the HTTP protocol or HHTPS protocol for the secured version.

Hostname: This contains the domain name or the IP address of the origin server.

Port: This part is optional and contains the port number on which the server is listening. If no port is given, the default port for the requested service is “80” for an HTTP URL.

2. When do browsers send the Origin header?

The Origin header is sent in the following cases:

  • All the cross origin requests, because all cross-origin requests have their response tainting set to “cors“. Except for cross-origin GET or HEAD requests if made in no-cors mode.
  • All same-origin requests (same-origin POSTOPTIONSPUTPATCH, and DELETE requests) except for GET or HEAD requests.

3. When do browsers set the origin to null?

Browsers must set an origin to null if the spec required to do so in the following cases:

The SpecCases when Origin is set to null
HTML spec:– Cross-origin images
– Cross-origin media data, including  video and audio.
– Documents generated from a data: URL.
– All iframes with sandbox attribute that doesn’t contain the value allow-same-origin
– Documents programmatically created using createDocument().
– Documents that do not have creator browsing context.
– Responses that are network errors.
 Fetch spec:– Redirects across origins
URL spec– For blob: URLs
– For file: URLs
– For any other URLs whose scheme is not one of httphttpsftpwswss, or gopher.

4. What is Same Origin

Some operations are restricted to same-origin content; Two requests have the same origin only when the scheme, hostname, and port are the same for both.

Request1 OriginRequest2 OriginSame Origin
http://originExample.com/app1/index.htmlhttp://originExample.com/app2/index.htmlYES
Same scheme: http.
Same hostname:originExample.com.
Different file path.
http://originExample.com:80
http://originExample.comYES
Port 80 is the default port for HTTP content.
Port 443 is the default port for HTTPS content.
http://originExample.com/app1https://originExample.com/app2NO
Different protocols: http and https.
http://originExample.comhttp://Example.comNO
Different hostNames.
http://originExample.com:8080http://originExample.comNO
Different ports.

Note that the same origin restriction can be lifted using CORS.

5. Get the Request Origin from the Origin header

To get the Request Origin from the headers in expess.js, add the following lines of code to the request:

  
  app.use((req, res, next) => {
   
   const origin = req.header('Origin');

  });


Or you can use:

 
   app.use((req, res, next) => {
    
   const origin = req.headers.origin;
 
  });

You can find a complete example of using request Origin in a node.js/express.js API here: Node.js + MySQL : Add Forgot/Reset Password to Login-Authentication System.

You might also like:

How to Build a Complete API for User Login and Authentication using MySQL and Node.js.

Node.js + MySQL : Add Forgot/Reset Password to Login-Authentication System.

How to store Session in MySQL Database using express-mysql-session.

How to interact with MySQL database using async/await promises in node.js ?

How to use Sequelize async/await to interact with MySQL database in Node.js.

MANY-TO-MANY Association in MYSQL Database using Sequelize async/await with Node.js.

ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js

ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js.

How to add Routes to insert data into MySQL database-related tables in Node.js API?

Example How to use initialize() Function in Node.js/Express API .

Why is Connection Pooling better than Single Connection?.

How to create MySQL database using node.js.

Leave a Comment

Your email address will not be published.

Translate »