How to configure Angular agents based on scaffolding
This article introduces the relevant knowledge of "how to configure Angular agents based on scaffolding". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Angular proxy configuration
Official website document https://angular.io/guide/build#using-corporate-proxy
Why do you do it?
Write an agent file and proxy the matching requests to other addresses to solve the cross-domain problem of local development.
How to configure it?
Create a proxy.config.js in the root directory
You can do the following configuration in this file
Add-- proxy-config proxy.config.js to the running project command of package.json
Configuration introduction
Const PROXY_CONFIG = [{context: ['/ api'], target: 'http://xxx', secure: false, changeOrigin: true, pathRewrite: {' ^ / api':',},},]; module.exports = PROXY_CONFIG
Context: path that needs to match
Target: the address to which the agent is represented
PathRewrite: rewrite the requested part of the path, which is an object, the key is the path to be rewritten by ^ +, and the value is the replacement path.
Secure: security settin
ChangeOrigin: changing sourc
Configure an instance
Such as http://localhost:4208/auth/login
Want to be an agent to
Http://www.baidu.com/news/login
It can be configured like this.
Const PROXY_CONFIG = [{context: ['/ auth/login'], target: 'http://www.baidu.com', pathRewrite: {' ^ / auth/login':'/ news/login',},},] module.exports = PROXY_CONFIG;Q: if there are two interfaces, one / api/cer/register and the other / api/cer/login, how can I proxy the two interfaces to different addresses? {context: ['/ api/cer/login'], target: 'xxx1', secure: false, changeOrigin: true,}, {context: [' / api'], target: 'xxx2', secure: false,}
Using / api, anyone who matches this will take its proxy, but if a more accurate / api/cer/login is added in front of it, it will match it first and take this proxy.
This is the end of "how to configure Angular agents based on scaffolding". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!