https://www.section.io/engineering-education/how-to-use-cors-in-nodejs-with-express/
Node.js is an open-source and cross-platform runtime used when executing JavaScript code on the server-side. One of the popular Node.js server frameworks is Express. Implementing CORS in Node.js helps you access numerous functionalities on the browser.
Node.js是一个在服务器端执行JavaScript代码时使用的开源跨平台运行时。Express是流行的Node.js服务器框架之一。在Node.js中实现CORS可以帮助您访问浏览器上的许多功能。
Express allows you to configure and manage an HTTP server to access resources from the same domain.
The three parts that form an origin are protocal, domain, and port.
image.png
Today, there are many applications that depend on APIs to access different resources. Some of the popular APIs include weather, time, and fonts. There are also servers that host these APIs and ensure that information is delivered to websites and other end points. Therefore, making cross-origin calls, is a popular use case for the modern web application.
Let’s say accessing images, videos, iframes, or scripts from another server. This means that the website is accessing resources from a different origin or domain. When building an application to serve up these resources with Express, a request to such external origins may fail. This is where CORS comes in to handle cross-origin requests.
Goal
This guide will help you learn how to configure CORS with Express.
Prerequisites
To follow this article along, prior knowledge of Node.js and Express is essential.
What is CORS?
CORS stands for Cross-Origin Resource Sharing
. It allows us to relax the security applied to an API. This is done by bypassing the Access-Control-Allow-Origin
headers, which specify which origins
can access the API.
In other words, CORS is a browser security feature that restricts cross-origin HTTP requests with other servers and specifies which domains access your resources.
Check this guide to learn more about the CORS policy.
网友评论