How to enable CORS in Nest.js

How to enable CORS in Nest.js (2022)A brief guide on how to enable CORS in Nest.jsIn this article, I want to quickly explain how to enable CORS in Nest.js without installing any additional package.What is CORS?CORS (Cross-Origin Resource Sharing) is a …


This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel

How to enable CORS in Nest.js (2022)

A brief guide on how to enable CORS in Nest.js

In this article, I want to quickly explain how to enable CORS in Nest.js without installing any additional package.

What is CORS?

CORS (Cross-Origin Resource Sharing) is a mechanism that uses an additional HTTP header to inform a browser to allow a web application running at one origin (domain) to have permission to access selected resources from a server at a different origin.

Requirements

It’s required to have a basic understanding of Node.js and TypeScript. I will choose Visual Studio Code as my code editor. You can use whatever you prefer.

Let’s get started

First, we are going to install the Nest.js CLI, so open the terminal of your choice and type:

$ npm i -g @nestjs/cli

We initialize a new Nest.js project with its CLI. That might take up to a minute.

$ nest new nest-cors -p npm

After this command is completed we can open our project in our code editor. Since I use Visual Studio Code, I am going to open the project by typing:

$ cd nest-cors
$ code .

My project looks like this in VSCode (Visual Studio Code):

Screenshot

Now, we can modify our src/main.ts file in order to enable CORS. Please change src/main.ts from:

to

Congratulations! We enabled CORS successfully. In addition, we could run our Nest.js application executing the following command in our terminal:

$ npm run start:dev

Thanks for reading my short guide on how to enable CORS in Nest.js.

Cheers

Read more articles about Nest.js

10x Development with Independent Components

Just like Microservices, Microfrontends are a great way to speed-up and scale app development, with independent deployments, decoupled codebases, and autonomous teams.

OSS Tools like Bit offer a great developer experience for building component-driven Micro frontends. Build independently versioned components, collaborate, and compose applications that scale. Give it a try →

An independent product component: watch the auto-generated dependency graph

Learn more


How to enable CORS in Nest.js was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel


Print Share Comment Cite Upload Translate Updates
APA

Kevin Vogel | Sciencx (2022-02-22T15:38:54+00:00) How to enable CORS in Nest.js. Retrieved from https://www.scien.cx/2022/02/22/how-to-enable-cors-in-nest-js/

MLA
" » How to enable CORS in Nest.js." Kevin Vogel | Sciencx - Tuesday February 22, 2022, https://www.scien.cx/2022/02/22/how-to-enable-cors-in-nest-js/
HARVARD
Kevin Vogel | Sciencx Tuesday February 22, 2022 » How to enable CORS in Nest.js., viewed ,<https://www.scien.cx/2022/02/22/how-to-enable-cors-in-nest-js/>
VANCOUVER
Kevin Vogel | Sciencx - » How to enable CORS in Nest.js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/22/how-to-enable-cors-in-nest-js/
CHICAGO
" » How to enable CORS in Nest.js." Kevin Vogel | Sciencx - Accessed . https://www.scien.cx/2022/02/22/how-to-enable-cors-in-nest-js/
IEEE
" » How to enable CORS in Nest.js." Kevin Vogel | Sciencx [Online]. Available: https://www.scien.cx/2022/02/22/how-to-enable-cors-in-nest-js/. [Accessed: ]
rf:citation
» How to enable CORS in Nest.js | Kevin Vogel | Sciencx | https://www.scien.cx/2022/02/22/how-to-enable-cors-in-nest-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.