MCPcopy
hub / github.com/react/create-react-app / prepareProxy

Function prepareProxy

packages/react-dev-utils/WebpackDevServerUtils.js:302–389  ·  view source on GitHub ↗
(proxy, appPublicFolder, servedPathname)

Source from the content-addressed store, hash-verified

300}
301
302function prepareProxy(proxy, appPublicFolder, servedPathname) {
303 // `proxy` lets you specify alternate servers for specific requests.
304 if (!proxy) {
305 return undefined;
306 }
307 if (typeof proxy !== 'string') {
308 console.log(
309 chalk.red('When specified, "proxy" in package.json must be a string.')
310 );
311 console.log(
312 chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".')
313 );
314 console.log(
315 chalk.red('Either remove "proxy" from package.json, or make it a string.')
316 );
317 process.exit(1);
318 }
319
320 // If proxy is specified, let it handle any request except for
321 // files in the public folder and requests to the WebpackDevServer socket endpoint.
322 // https://github.com/facebook/create-react-app/issues/6720
323 const sockPath = process.env.WDS_SOCKET_PATH || '/ws';
324 const isDefaultSockHost = !process.env.WDS_SOCKET_HOST;
325 function mayProxy(pathname) {
326 const maybePublicPath = path.resolve(
327 appPublicFolder,
328 pathname.replace(new RegExp('^' + servedPathname), '')
329 );
330 const isPublicFileRequest = fs.existsSync(maybePublicPath);
331 // used by webpackHotDevClient
332 const isWdsEndpointRequest =
333 isDefaultSockHost && pathname.startsWith(sockPath);
334 return !(isPublicFileRequest || isWdsEndpointRequest);
335 }
336
337 if (!/^http(s)?:\/\//.test(proxy)) {
338 console.log(
339 chalk.red(
340 'When "proxy" is specified in package.json it must start with either http:// or https://'
341 )
342 );
343 process.exit(1);
344 }
345
346 let target;
347 if (process.platform === 'win32') {
348 target = resolveLoopback(proxy);
349 } else {
350 target = proxy;
351 }
352 return [
353 {
354 target,
355 logLevel: 'silent',
356 // For single page apps, we generally want to fallback to /index.html.
357 // However we also want to respect `proxy` for API calls.
358 // So if `proxy` is specified as a string, we need to decide which fallback to use.
359 // We use a heuristic: We want to proxy all the requests that are not meant

Callers 1

start.jsFile · 0.85

Calls 4

resolveLoopbackFunction · 0.85
mayProxyFunction · 0.85
onProxyErrorFunction · 0.85
testMethod · 0.80

Tested by

no test coverage detected