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):
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 →
Learn more
- Building a React Component Library — The Right Way
- 7 Tools for Faster Frontend Development in 2022
- Microservices are Dead — Long Live Miniservices
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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.