From df08ff5f493bf05aff2484a9d9440f1173780f97 Mon Sep 17 00:00:00 2001 From: Matthew Alner Date: Sun, 25 Nov 2018 12:00:49 +0000 Subject: [PATCH] Add documentation for setting up proxy for Angular CLI --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/README.md b/README.md index ae899821..9c66790a 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,51 @@ module.exports = { The serving port can be changed with the `-p`/`--port` option. +## Using with `Angular CLI` + +CORS issues when trying to use netlify-lambdas locally with angular? you need to set up a proxy. + +Firstly make sure you are using relative paths in your app to ensure that your app will work locally and on Netlify, example below... + +```js + this.http.get('/.netlify/functions/jokeTypescript') +``` + +Then place a `proxy.config.json` file in the root of your project, the contents should look something like... + +```json +{ + "/.netlify/functions/*": { + "target": "http://localhost:9000", + "secure": false, + "logLevel": "debug", + "changeOrigin": true + } +} +``` + +- The `key` should match up with the location of your Transpiled `functions` as defined in your `netlify.toml` +- The `target` should match the port that the lambdas are being served on (:9000 by default) + +When you run up your Angular project you need to pass in the proxy config with the flag `--proxy-config` like so... + +```bash + ng serve --proxy-config proxy.config.json +``` + +To make your life easier you can add these to your `scripts` in `package.json` + +```json + "scripts": { + "start": "ng serve --proxy-config proxy.config.json", + "build": "ng build --prod --aot && yarn nlb", + "nls": "netlify-lambda serve src_functions", + "nlb": "netlify-lambda build src_functions" + } +``` + +Obviously you need to run up `netlify-lambda` & `angular` at the same time. + ## Webpack Configuration By default the webpack configuration uses `babel-loader` to load all js files. Any `.babelrc` in the directory `netlify-lambda` is run from will be respected. If no `.babelrc` is found, a [few basic settings are used](https://github.com/netlify/netlify-lambda/blob/master/lib/build.js#L11-L15a).