Skip to content
the poor coder

only allow requests from domain express

Poor Man
Published
Reading time
2 min read
On this page

How to Only Allow Requests from Domain Express#

If you want to restrict your server to only accept requests from a specific domain, you can achieve this by configuring your server to verify the "Origin" header of incoming requests.

Method 1: Using CORS Middleware#

If you are using a Node.js-based framework like Express, you can use the built-in CORS middleware to set up this restriction. Here's an example:

Text

const express = require('express');
const cors = require('cors');

const app = express();

const allowedOrigins = ['https://example.com'];

const corsOptions = {
  origin: (origin, callback) => {
    if (allowedOrigins.indexOf(origin) !== -1) {
      callback(null, true);
    } else {
      callback(new Error('Not allowed by CORS'));
    }
  },
};

app.use(cors(corsOptions));

// rest of your routes

In this example, we're specifying that the server should only allow requests from the domain "https://example.com". If any other domain tries to send a request, the server will respond with a 403 error.

Method 2: Implementing Your Own Middleware#

If you prefer not to use the built-in CORS middleware, you can implement your own middleware function that checks the "Origin" header and rejects requests from unauthorized domains. Here's an example:

Text

const express = require('express');

const app = express();

const allowedOrigins = ['https://example.com'];

app.use((req, res, next) => {
  const { origin } = req.headers;
  if (allowedOrigins.indexOf(origin) !== -1) {
    res.setHeader('Access-Control-Allow-Origin', origin);
    next();
  } else {
    res.status(403).send('Forbidden');
  }
});

// rest of your routes

In this example, we're checking the "Origin" header of incoming requests and sending a 403 error if the domain is not authorized. If the domain is authorized, we're setting the "Access-Control-Allow-Origin" header to the value of the incoming "Origin" header, which allows the request to proceed.

Both of these methods achieve the same result, so it's up to you to decide which one works best for your situation.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC