Created
September 13, 2020 10:35
-
-
Save justinehell/3c3b90cd6ea68bbd93a992e96ef40bad to your computer and use it in GitHub Desktop.
Express 3 - Méthode POST et insertion de données
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
const port = 3000; | |
const connection = require("./conf"); | |
app.use(express.json()); | |
app.use( | |
express.urlencoded({ | |
extended: true, | |
}) | |
); | |
app.post("/api/movies", (req, res) => { | |
const formData = req.body; | |
connection.query("INSERT INTO movie SET ?", formData, (err, results) => { | |
if (err) { | |
console.log(err); | |
res.status(500).send("Error when saving a movie"); | |
} else { | |
res.sendStatus(200); | |
} | |
}); | |
}); | |
app.listen(port, (err) => { | |
if (err) { | |
throw new Error("Something bad happened..."); | |
} | |
console.log(`Server is listening on ${port}`); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment