Skip to content

Commit

Permalink
add express example
Browse files Browse the repository at this point in the history
  • Loading branch information
alagrede committed Oct 9, 2022
1 parent 0096bb2 commit a9b6e87
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
72 changes: 72 additions & 0 deletions examples/express-demo.md
@@ -0,0 +1,72 @@
# Express client/server demo
Example of node backend with express and client with fetch

## Installation
Install NPM packages
`npm i -S express`
`npm i -S body-parser`

# Server
```js
//exec: /usr/local/opt/node@14/bin/node
const express = require('express')
const bodyParser = require('body-parser');
const app = express()

app.get('/', function (req, res) {
res.send('Hello World')
})
// POST HTML Form
app.post('/subscribe',
express.urlencoded({extended: true}),
async (request, response) => {
print(`email ${request.body.email}`)
response.sendStatus(200);
})

// POST JSON data
app.post('/activate', bodyParser.json(), async (request, response) => {
printJSON(request.body);
response.sendStatus(200);
})

app.listen(4000)
```


# Client
## Post form with JS
```js
const data = new URLSearchParams();
data.append('email', 'name@domain.com');

const result = await fetch("http://localhost:4000/subscribe", {
body: data,
method: "post"
});
print(await result.text())
```

## Post form with HTML
```html form
<form action="http://localhost:4000/contact" method="POST">
Email: <input type="text" name="email" id="email" required>
<button type="submit">Submit</button>
</form>
```

```js
const form = loadBlock('form')
print(form)
```

## POST JSON
```js
const data = {name: "Anthony"}
const result = await fetch("http://localhost:4000/activate", {
body: JSON.stringify(data),
method: "post",
headers: {'Content-Type': 'application/json'}
});
print(await result.text())
```

0 comments on commit a9b6e87

Please sign in to comment.