preserve indentation
This commit is contained in:
parent
814533be85
commit
2aeba61e73
1 changed files with 37 additions and 37 deletions
74
README.md
74
README.md
|
|
@ -252,15 +252,15 @@ Lets rock and roll.
|
||||||
{
|
{
|
||||||
"name": "netlify-fauna",
|
"name": "netlify-fauna",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
# scaffold and setup FaunaDB
|
"ABOUT-bootstrap-command": "#👇 scaffold and setup FaunaDB #",
|
||||||
"bootstrap": "node ./scripts/bootstrap-fauna-database.js",
|
"bootstrap": "node ./scripts/bootstrap-fauna-database.js",
|
||||||
# start the app and server
|
"ABOUT-start-command": "#👇 start the app and server #",
|
||||||
"start": "npm-run-all --parallel start:app start:server",
|
"start": "npm-run-all --parallel start:app start:server",
|
||||||
"start:app": "react-scripts start",
|
"start:app": "react-scripts start",
|
||||||
"start:server": "netlify-lambda serve functions -c ./webpack.config.js",
|
"start:server": "netlify-lambda serve functions -c ./webpack.config.js",
|
||||||
# before 'build' runs, run the 'bootstrap' command
|
"ABOUT-prebuild-command": "#👇 before 'build' runs, run the 'bootstrap' command #",
|
||||||
"prebuild": "echo 'setup faunaDB' && npm run bootstrap",
|
"prebuild": "echo 'setup faunaDB' && npm run bootstrap",
|
||||||
# build the react app and the serverless functions
|
"ABOUT-build-command": "#👇 build the react app and the serverless functions #",
|
||||||
"build": "npm-run-all --parallel build:**",
|
"build": "npm-run-all --parallel build:**",
|
||||||
"build:app": "react-scripts build",
|
"build:app": "react-scripts build",
|
||||||
"build:functions": "netlify-lambda build functions -c ./webpack.config.js",
|
"build:functions": "netlify-lambda build functions -c ./webpack.config.js",
|
||||||
|
|
@ -300,44 +300,44 @@ Lets rock and roll.
|
||||||
Then create a new function file in `/functions` called `todos-create.js`
|
Then create a new function file in `/functions` called `todos-create.js`
|
||||||
|
|
||||||
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./functions/todos-create.js) -->
|
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./functions/todos-create.js) -->
|
||||||
<!-- The below code snippet is automatically added from ./functions/todos-create.js -->
|
<!-- The below code snippet is automatically added from ./functions/todos-create.js -->
|
||||||
```js
|
```js
|
||||||
/* Import faunaDB sdk */
|
/* Import faunaDB sdk */
|
||||||
import faunadb from 'faunadb'
|
import faunadb from 'faunadb'
|
||||||
|
|
||||||
const q = faunadb.query
|
const q = faunadb.query
|
||||||
const client = new faunadb.Client({
|
const client = new faunadb.Client({
|
||||||
secret: process.env.FAUNADB_SECRET
|
secret: process.env.FAUNADB_SECRET
|
||||||
})
|
})
|
||||||
|
|
||||||
exports.handler = (event, context, callback) => {
|
exports.handler = (event, context, callback) => {
|
||||||
const data = JSON.parse(event.body)
|
const data = JSON.parse(event.body)
|
||||||
console.log("Function `todo-create` invoked", data)
|
console.log("Function `todo-create` invoked", data)
|
||||||
const todoItem = {
|
const todoItem = {
|
||||||
data: data
|
data: data
|
||||||
}
|
}
|
||||||
/* construct the fauna query */
|
/* construct the fauna query */
|
||||||
return client.query(q.Create(q.Ref("classes/todos"), todoItem))
|
return client.query(q.Create(q.Ref("classes/todos"), todoItem))
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
console.log("success", response)
|
console.log("success", response)
|
||||||
return callback(null, {
|
return callback(null, {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
body: JSON.stringify(response)
|
body: JSON.stringify(response)
|
||||||
})
|
})
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
console.log("error", error)
|
console.log("error", error)
|
||||||
return callback(null, {
|
return callback(null, {
|
||||||
statusCode: 400,
|
statusCode: 400,
|
||||||
body: JSON.stringify(error)
|
body: JSON.stringify(error)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
<!-- AUTO-GENERATED-CONTENT:END -->
|
<!-- AUTO-GENERATED-CONTENT:END -->
|
||||||
|
|
||||||
### 3. Connect the function to the frontend app
|
### 3. Connect the function to the frontend app
|
||||||
|
|
||||||
Inside of your react app. You can now wire up the
|
Inside of your react app. You can now wire up the `/.netlify/functions/todos-create` endpoint to an AJAX request
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Function using fetch to POST to our API endpoint
|
// Function using fetch to POST to our API endpoint
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue