Skip to content

Commit 89f4a30

Browse files
author
sw-yx
committed
init
0 parents  commit 89f4a30

17 files changed

+9235
-0
lines changed

.gitignore

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
/lambda
12+
13+
# misc
14+
.DS_Store
15+
.env.local
16+
.env.development.local
17+
.env.test.local
18+
.env.production.local
19+
20+
npm-debug.log*
21+
yarn-debug.log*
22+
yarn-error.log*

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
This project is based on [Create React App v2](https://github.com/facebookincubator/create-react-app) and [netlify-lambda v1](https://github.com/netlify/netlify-lambda). (For more information about Create react App, check their full [documentation](https://github.com/facebookincubator/create-react-app#create-react-app).)
2+
3+
The main addition is a new folder: `src/lambda`. Each JavaScript file in there will automatically be prepared for Lambda function deployment.
4+
5+
As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`.
6+
7+
[![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/netlify/create-react-app-lambda)
8+
9+
## Babel/webpack compilation
10+
11+
All functions are compiled with webpack using the Babel Loader, so you can use modern JavaScript, import npm modules, etc., without any extra setup.
12+
13+
## Local Development
14+
15+
Before developing, clone the repository and run `yarn` from the root of the repo to install all dependencies.
16+
17+
### Run the functions dev server
18+
19+
From inside the project folder, run:
20+
21+
```
22+
yarn start:lambda
23+
```
24+
25+
This will open a local server running at `http://localhost:9000` serving your Lambda functions, updating as you make changes in the `src/lambda` folder.
26+
27+
You can then access your functions directly at `http://localhost:9000/{function_name}`, but to access them with the app, you'll need to start the app dev server. Under the hood, this uses `react-scripts`' [advanced proxy feature](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#configuring-the-proxy-manually) with the `setupProxy.js` file.
28+
29+
### Run the app dev server
30+
31+
While the functions server is still running, open a new terminal tab and run:
32+
33+
```
34+
yarn start
35+
```
36+
37+
This will start the normal create-react-app dev server and open your app at `http://localhost:3000`.
38+
39+
Local in-app requests to the relative path `/.netlify/functions/*` will automatically be proxied to the local functions dev server.

netlify.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[build]
2+
Command = "yarn build && yarn build:lambda"
3+
Functions = "lambda"
4+
Publish = "build"

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "react-lambda",
3+
"version": "0.4.0",
4+
"private": true,
5+
"dependencies": {
6+
"react": "^16.5.2",
7+
"react-dom": "^16.5.2",
8+
"react-scripts": "2.0.4"
9+
},
10+
"scripts": {
11+
"start": "run-p start:**",
12+
"start:app": "react-scripts start",
13+
"start:lambda": "netlify-lambda serve src/lambda",
14+
"build": "run-p build:**",
15+
"build:app": "react-scripts build",
16+
"build:lambda": "netlify-lambda build src/lambda",
17+
"test": "react-scripts test",
18+
"eject": "react-scripts eject"
19+
},
20+
"eslintConfig": {
21+
"extends": "react-app"
22+
},
23+
"browserslist": [
24+
">0.2%",
25+
"not dead",
26+
"not ie <= 11",
27+
"not op_mini all"
28+
],
29+
"devDependencies": {
30+
"babel-loader": "^8.0.4",
31+
"http-proxy-middleware": "^0.19.0",
32+
"netlify-lambda": "^1.0.1",
33+
"npm-run-all": "^4.1.3"
34+
}
35+
}

public/favicon.ico

3.78 KB
Binary file not shown.

public/index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<meta name="theme-color" content="#000000">
7+
<!--
8+
manifest.json provides metadata used when your web app is added to the
9+
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
10+
-->
11+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
12+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
13+
<!--
14+
Notice the use of %PUBLIC_URL% in the tags above.
15+
It will be replaced with the URL of the `public` folder during the build.
16+
Only files inside the `public` folder can be referenced from the HTML.
17+
18+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
19+
work correctly both with client-side routing and a non-root public URL.
20+
Learn how to configure a non-root public URL by running `npm run build`.
21+
-->
22+
<title>React App</title>
23+
</head>
24+
<body>
25+
<noscript>
26+
You need to enable JavaScript to run this app.
27+
</noscript>
28+
<div id="root"></div>
29+
<!--
30+
This HTML file is a template.
31+
If you open it directly in the browser, you will see an empty page.
32+
33+
You can add webfonts, meta tags, or analytics to this file.
34+
The build step will place the bundled scripts into the <body> tag.
35+
36+
To begin the development, run `npm start` or `yarn start`.
37+
To create a production bundle, use `npm run build` or `yarn build`.
38+
-->
39+
</body>
40+
</html>

public/manifest.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
}
10+
],
11+
"start_url": "./index.html",
12+
"display": "standalone",
13+
"theme_color": "#000000",
14+
"background_color": "#ffffff"
15+
}

src/App.css

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.App {
2+
text-align: center;
3+
}
4+
5+
.App-logo {
6+
animation: App-logo-spin infinite 20s linear;
7+
height: 40vmin;
8+
}
9+
10+
.App-header {
11+
background-color: #282c34;
12+
min-height: 100vh;
13+
display: flex;
14+
flex-direction: column;
15+
align-items: center;
16+
justify-content: center;
17+
font-size: calc(10px + 2vmin);
18+
color: white;
19+
}
20+
21+
.App-link {
22+
color: #61dafb;
23+
}
24+
25+
@keyframes App-logo-spin {
26+
from {
27+
transform: rotate(0deg);
28+
}
29+
to {
30+
transform: rotate(360deg);
31+
}
32+
}

src/App.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React, { Component } from 'react';
2+
import logo from './logo.svg';
3+
import './App.css';
4+
5+
class LambdaDemo extends Component {
6+
constructor(props) {
7+
super(props);
8+
this.state = { loading: false, msg: null };
9+
}
10+
11+
handleClick = e => {
12+
e.preventDefault();
13+
14+
this.setState({ loading: true });
15+
fetch('/.netlify/functions/hello')
16+
.then(response => response.json())
17+
.then(json => this.setState({ loading: false, msg: json.msg }));
18+
};
19+
20+
render() {
21+
const { loading, msg } = this.state;
22+
23+
return (
24+
<p>
25+
<button onClick={this.handleClick}>
26+
{loading ? 'Loading...' : 'Call Lambda'}
27+
</button>
28+
<br />
29+
<span>{msg}</span>
30+
</p>
31+
);
32+
}
33+
}
34+
35+
class App extends Component {
36+
render() {
37+
return (
38+
<div className="App">
39+
<header className="App-header">
40+
<img src={logo} className="App-logo" alt="logo" />
41+
<p>
42+
Edit <code>src/App.js</code> and save to reload.
43+
</p>
44+
<LambdaDemo />
45+
</header>
46+
</div>
47+
);
48+
}
49+
}
50+
51+
export default App;

src/App.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
it('renders without crashing', () => {
6+
const div = document.createElement('div');
7+
ReactDOM.render(<App />, div);
8+
ReactDOM.unmountComponentAtNode(div);
9+
});

src/index.css

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
5+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
6+
sans-serif;
7+
-webkit-font-smoothing: antialiased;
8+
-moz-osx-font-smoothing: grayscale;
9+
}
10+
11+
code {
12+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
13+
monospace;
14+
}

src/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import './index.css';
4+
import App from './App';
5+
import * as serviceWorker from './serviceWorker';
6+
7+
ReactDOM.render(<App />, document.getElementById('root'));
8+
9+
// If you want your app to work offline and load faster, you can change
10+
// unregister() to register() below. Note this comes with some pitfalls.
11+
// Learn more about service workers: http://bit.ly/CRA-PWA
12+
serviceWorker.unregister();

src/lambda/hello.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// show object spread works, i.e. babel works
2+
const obj = {
3+
foo: 'bar'
4+
};
5+
export function handler(event, context, callback) {
6+
console.log('qsp', event.queryStringParameters);
7+
console.log('**********');
8+
console.log('mvsp', event.multiValueQueryStringParameters);
9+
callback(null, {
10+
statusCode: 200,
11+
body: JSON.stringify({ msg: 'Hello, World!', ...obj })
12+
});
13+
}

src/logo.svg

+7
Loading

0 commit comments

Comments
 (0)