Skip to content
the poor coder

nestjs allow origin

Poor Man
Published
Reading time
2 min read
On this page

What is NestJS Allow Origin?#

Have you ever encountered an error message saying "Access to XMLHttpRequest has been blocked by CORS policy"? This usually happens when you try to make an AJAX request from a different domain. In order to prevent cross-site scripting attacks, browsers have implemented the Same Origin Policy, which restricts web pages from making requests to a different domain than the one that served the web page.

To bypass this restriction, we need to enable Cross-Origin Resource Sharing (CORS) on the server-side. This is where NestJS Allow Origin comes into play. NestJS provides a built-in middleware called 'CorsMiddleware' that can be used to enable CORS.

How to enable NestJS Allow Origin?#

To enable CORS in your NestJS application, you need to do the following:

  1. Import the CorsMiddleware from @nestjs/common package and add it to the AppModule.
  2. Call the .enable() method on the CorsMiddleware instance and pass in an options object with the desired configuration.

Here's an example of how to use the CorsMiddleware:

JavaScript

import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CorsMiddleware } from '@nestjs/common';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(CorsMiddleware)
      .forRoutes('*');
  }
}

This code will enable CORS for all routes in your application. You can also configure the CorsMiddleware to allow only specific origins, methods, and headers. Here's an example:

JavaScript

consumer
  .apply(CorsMiddleware)
  .forRoutes('*')
  .with({
    origin: 'http://localhost:3000',
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    preflightContinue: false,
    optionsSuccessStatus: 204,
    credentials: true,
  });

Here, we're allowing requests from http://localhost:3000, and specifying the allowed HTTP methods, as well as some other options.

Conclusion#

NestJS Allow Origin is a powerful middleware that can help you bypass the Same Origin Policy and enable CORS in your application. By using NestJS CorsMiddleware, you can easily configure your server to allow requests from any domain or specific domains, and specify which HTTP methods and headers are allowed. With NestJS, you can be sure that your application is secure and compliant with web standards.

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