Last active
February 17, 2021 11:17
-
-
Save yostane/52bdc7f88b18238380e0f26a10c29ca7 to your computer and use it in GitHub Desktop.
mysql node
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 mariadb = require('mariadb'); | |
const pool = mariadb.createPool({ | |
host: 'mydb.com', | |
user:'myUser', | |
password: 'myPassword', | |
connectionLimit: 5 | |
}); |
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
var mysql = require('mysql'); | |
var connection = mysql.createConnection({ | |
host : 'localhost', | |
user : 'me', | |
password : 'secret', | |
database : 'my_db' | |
}); |
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
app.get("/crew", (req, res) => { | |
const query = "select name, job from crewmates"; | |
connection.query(query, (error, results, fields) => { | |
if (error) throw error; | |
console.log("Résultat", results); | |
res.json(results); | |
}); | |
}); |
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
app.post("/crew", (req, res) => { | |
const query = "INSERT INTO crewmates SET ?"; | |
const value = { name: req.body.name, job: req.body.job }; | |
connection.query(query, value, (error, results, fields) => { | |
if (error) throw error; | |
console.log("Résultat de l'insert", results); | |
res.sendStatus(200); | |
}); | |
}); |
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
POST http://localhost:3000/crew | |
Content-Type: application/json | |
{"name": "sprirou", "job": "impostor"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment