Skip to content

Commit

Permalink
Merge pull request #7 from wh1zk1d/add-nodejs-example
Browse files Browse the repository at this point in the history
Add Node.js example
  • Loading branch information
tewarig authored Oct 3, 2021
2 parents e3dae55 + e8caa0d commit fc7eada
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions docs/Node.js example/nodejs_example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# How to use MeowForms with Node.js

First, make sure you have signed up at [MeowForms](https://meowform.xyz) and created a form.

## Requirements

- Node.js
- Express.js
- Axios

To handle your form data with Node.js, you must send them from the frontend by using a POST request. A basic HTML form could look like this:

```html
<!-- "/handleform" is the name of the route that will process the form data -->
<form action="/handleform" method="POST">
<label for="email">E-Mail</label>
<input type="email" name="email" id="email" />

<label for="name">E-Mail</label>
<input type="text" name="name" id="name" />

<button type="submit">Send</button>
</form>
```

When you use Express.js, you need to add the following code to add a route that serves the HTML file with the form:

```javascript
/* "/form" is the name of the route that serves the form */
app.get('/form', (req, res) => {
res.sendFile(__dirname + '/form.html') /* form.html is the name of your HTML file */
})
```

Next, you need to enable Express.js to parse JSON bodies and URL-encoded bodies:

```javascript
app.use(express.json())
app.use(express.urlencoded())
```

Finally, add the code for the route to process the form data and send it to MeowForm:

```javascript
app.post('/handleform', async (req, res) => {
/* Destructure form values from the request body */
const { name, email } = req.body

/* Send the values to MeowForm with another POST request */
const request = await axios({
method: 'post',
/* Replace [email protected] with your email and SomeForm with the name of your form */
url: 'https://server.meowform.xyz/form/[email protected]&SomeForm',
data: {
name,
email,
},
})

if (request.status === 200) {
res.status(200).json({ message: 'success' })
} else {
res.status(500).json({ message: 'an error occured' })
}
})
```

1 comment on commit fc7eada

@vercel
Copy link

@vercel vercel bot commented on fc7eada Oct 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.