|
| 1 | +# 17 proxy |
| 2 | + |
| 3 | +In this demo we are going to configure Webpack proxy, proxying some URLs can be useful when you have a separate API backend development server and you want to send API requests on the same domain. |
| 4 | + |
| 5 | +We will start from sample _16-bundle-analyzer. |
| 6 | + |
| 7 | +Summary steps: |
| 8 | + |
| 9 | +- Configure _webpack.config_ to send requests from the app's domain to the API. |
| 10 | +- Modify webpack.dev.js. |
| 11 | +- Modify webpack.prod.js. |
| 12 | +- Create script apiTest.tsx. |
| 13 | +- Modify index.tsx |
| 14 | + |
| 15 | +# Steps to build it |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +Prerequisites, you will need to have nodejs installed in your computer. If you want to follow this step guides you will need to take as starting point sample _16-bundle-analyzer. |
| 20 | + |
| 21 | +## Steps |
| 22 | + |
| 23 | +- `npm install` to install previous sample packages: |
| 24 | + |
| 25 | +``` |
| 26 | +npm install |
| 27 | +``` |
| 28 | + |
| 29 | +- Now is the time to modify the performance configuration file. |
| 30 | + |
| 31 | +_./webpack.common.js_ |
| 32 | + |
| 33 | +```diff |
| 34 | +module.exports = { |
| 35 | + .... |
| 36 | + plugins: [ |
| 37 | + new CleanWebpackPlugin(), |
| 38 | + //Generate index.html in /dist => https://github.com/ampedandwired/html-webpack-plugin |
| 39 | + new HtmlWebpackPlugin({ |
| 40 | + filename: "index.html", //Name of file in ./dist/ |
| 41 | + template: "index.html", //Name of template in ./src |
| 42 | + }), |
| 43 | + new MiniCssExtractPlugin({ |
| 44 | + filename: "[name].css", |
| 45 | + chunkFilename: "[id].css", |
| 46 | + }), |
| 47 | + ], |
| 48 | ++ devServer: { |
| 49 | ++ proxy: { |
| 50 | ++ "/api": { |
| 51 | ++ //The origin of the host header is kept when proxying by default: if true to override this behaviour. |
| 52 | ++ //Use when is name-based hosted sites. |
| 53 | ++ "changeOrigin": true, |
| 54 | ++ //If you don't want /api to be passed along, we need to rewrite the path: |
| 55 | ++ pathRewrite: { "^/api": "" }, |
| 56 | ++ // If you want filter the request type |
| 57 | ++ bypass: function(req, res, proxyOptions) { |
| 58 | ++ if(req.method != 'GET') return false; |
| 59 | ++ } |
| 60 | ++ }, |
| 61 | ++ "/get": { |
| 62 | ++ //The origin of the host header is kept when proxying by default: if true to override this behaviour. |
| 63 | ++ //Use when is name-based hosted sites. |
| 64 | ++ "changeOrigin": true, |
| 65 | ++ //If you don't want /api/get to be passed along, we need to rewrite the path: |
| 66 | ++ pathRewrite: { "^/api/get": "" }, |
| 67 | ++ // If you want filter the request type |
| 68 | ++ bypass: function(req, res, proxyOptions) { |
| 69 | ++ if(req.method != 'GET') return false; |
| 70 | ++ } |
| 71 | ++ } |
| 72 | ++ } |
| 73 | ++ } |
| 74 | +}; |
| 75 | +``` |
| 76 | + |
| 77 | +- Now modifiy _webpack.dev.js_ |
| 78 | + |
| 79 | +```diff |
| 80 | +module.exports = merge(common, { |
| 81 | + mode: "development", |
| 82 | + devtool: "inline-source-map", |
| 83 | + devServer: { |
| 84 | + stats: "errors-only", |
| 85 | + }, |
| 86 | + plugins: [ |
| 87 | + new Dotenv({ |
| 88 | + path: "./dev.env", |
| 89 | + }), |
| 90 | + ], |
| 91 | ++ devServer: { |
| 92 | ++ proxy: { |
| 93 | ++ "/api": { |
| 94 | ++ target: "https://httpbin.org/", |
| 95 | ++ }, |
| 96 | ++ "/get": { |
| 97 | ++ target: "https://httpbin.org/", |
| 98 | ++ } |
| 99 | ++ } |
| 100 | ++ }, |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +- Now modifiy _webpack.dev.js_ |
| 105 | +```diff |
| 106 | +module.exports = merge(common, { |
| 107 | + mode: "development", |
| 108 | + devtool: "inline-source-map", |
| 109 | + devServer: { |
| 110 | + stats: "errors-only", |
| 111 | + }, |
| 112 | + plugins: [ |
| 113 | + new Dotenv({ |
| 114 | + path: "./dev.env", |
| 115 | + }), |
| 116 | + ], |
| 117 | ++ devServer: { |
| 118 | ++ proxy: { |
| 119 | ++ "/api": { |
| 120 | ++ target: "https://myApi.com", |
| 121 | ++ }, |
| 122 | ++ "/get": { |
| 123 | ++ target: "https://myApi.com", |
| 124 | ++ } |
| 125 | ++ } |
| 126 | ++ }, |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +- Now create a new file _apiTest.tsx_ |
| 131 | + |
| 132 | +```javascript |
| 133 | +import React from "react"; |
| 134 | +const reqGet = (() => { |
| 135 | + let status = "pending"; |
| 136 | + let result; |
| 137 | + const resultData = fetch("api/get") |
| 138 | + .then(function (response) { |
| 139 | + return response.json(); |
| 140 | + }) |
| 141 | + .then(function (data) { |
| 142 | + status = "success"; |
| 143 | + console.log(data); |
| 144 | + result = data; |
| 145 | + }) |
| 146 | + .catch(error => { |
| 147 | + status = "error"; |
| 148 | + result = `${status} ${error}`; |
| 149 | + }); |
| 150 | + |
| 151 | + return { |
| 152 | + Request() { |
| 153 | + if (status === "pending") { |
| 154 | + console.log(status); |
| 155 | + throw resultData; |
| 156 | + } else if (status === "error") { |
| 157 | + console.log(status); |
| 158 | + return result; |
| 159 | + } else if (status === "success") { |
| 160 | + console.log(status); |
| 161 | + return result; |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +})() |
| 166 | + |
| 167 | +function getListObject(obj) { |
| 168 | + return ( |
| 169 | + <ul> |
| 170 | + {Object.keys(obj).map((keyOb) => |
| 171 | + (<li> |
| 172 | + {keyOb}: |
| 173 | + {typeof obj[keyOb] === "object" ? |
| 174 | + getListObject(obj[keyOb]) : |
| 175 | + obj[keyOb]} |
| 176 | + </li>))} |
| 177 | + </ul> |
| 178 | + ) |
| 179 | +} |
| 180 | + |
| 181 | +export const RequestGet = () => { |
| 182 | + let obj = reqGet.Request(); |
| 183 | + return (<div> |
| 184 | + <h5>Result API http://localhost:8080/api/get: </h5> |
| 185 | + {typeof obj === "object" ? getListObject(obj) : obj} |
| 186 | + </div>); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +Finally, we need to update _index.tsx_: |
| 191 | + |
| 192 | +```diff |
| 193 | +import React, { Suspense } from "react"; |
| 194 | +import ReactDOM from "react-dom"; |
| 195 | +++ import { RequestGet } from "./apiTest"; |
| 196 | +import { AverageComponent } from "./averageComponent"; |
| 197 | +import { TotalScoreComponent } from './totalScoreComponent'; |
| 198 | + |
| 199 | +ReactDOM.render( |
| 200 | + <div> |
| 201 | + <h1>Hello from React DOM</h1> |
| 202 | + <AverageComponent /> |
| 203 | + <TotalScoreComponent /> |
| 204 | +++ <Suspense fallback={<h1>Loading ...</h1>}> |
| 205 | +++ <RequestGet /> |
| 206 | +++ </Suspense> |
| 207 | + </div>, |
| 208 | + document.getElementById("root") |
| 209 | +); |
| 210 | +``` |
| 211 | + |
| 212 | +- Now we execute the command `npm start` |
| 213 | + |
| 214 | +```bash |
| 215 | +npm start |
| 216 | +``` |
| 217 | + |
| 218 | +# About Basefactor + Lemoncode |
| 219 | + |
| 220 | +We are an innovating team of Javascript experts, passionate about turning your ideas into robust products. |
| 221 | + |
| 222 | +[Basefactor, consultancy by Lemoncode](http://www.basefactor.com) provides consultancy and coaching services. |
| 223 | + |
| 224 | +[Lemoncode](http://lemoncode.net/services/en/#en-home) provides training services. |
| 225 | + |
| 226 | +For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend |
0 commit comments