Cross-domain problem
An egg – cors open:
An egg – cors configuration:
This is one of the most commonly used plug-ins to solve cross-domain problems.
However, in my actual project, some requirements cannot be met, so I think egg-CORS has certain limitations in configuration.
- The orgin parameter can only be set to a domain name, or ‘*’.
- When set to ‘*’ to allow all domain names to cross domains, the request with withCredentials: true still has a cross-domain problem.
In order to solve the above problems, DECIDED to do their own plugin egg-Origin. This is middleware and the code is very simple.
Yes, the above is all to the code 😅😅
Setthis.ctx.response. set(‘ access-Control-allow-origin ‘, origin); In this way, you can configure the cross-domain whitelist based on your requirements.
Usage:
Use the same as egg-cors
An egg – origin to open:
An egg – origin configuration:
The whiteList parameter can be set to a domain name or [‘ * ‘]. It must be an array, so as to maintain the same configuration convention as egg-cors.
Egg-origin whitelist overrides the egg-cors configuration. Standing on the shoulders of giants, to see further, thanks to the giant.
Late Iteration plan
At present, only the full matching mode is supported. The next iteration plan includes the configuration of level 1 domain names, all sub-domain names can be used, and the “blacklist” configuration will be added to filter certain domain names.