Skip to content

Instantly share code, notes, and snippets.

@vmalep
Created November 8, 2021 14:28
Show Gist options
  • Save vmalep/941173acd3c708caa6184e3872770f91 to your computer and use it in GitHub Desktop.
Save vmalep/941173acd3c708caa6184e3872770f91 to your computer and use it in GitHub Desktop.
app.post('/api/users', (req, res) => {
const { firstname, lastname, email } = req.body
connection.promise().query(
'INSERT INTO users(firstname, lastname, email) VALUES (?, ?, ?)',
[firstname, lastname, email],)
.then(result=>res.status(201).send('User successfully saved')
.catch(err=>res.status(500).send('Error saving the user'))
)
})
// If needed to check the result
app.get('/api/users', (req, res) => {
connection.promise().query('SELECT * FROM users')
.then(result=>res.status(200).json(result))
.catch(err=>res.status(500).send('Error retrieving data from database'))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment