Skip to content

Instantly share code, notes, and snippets.

@yostane
Last active February 17, 2021 11:17
Show Gist options
  • Save yostane/52bdc7f88b18238380e0f26a10c29ca7 to your computer and use it in GitHub Desktop.
Save yostane/52bdc7f88b18238380e0f26a10c29ca7 to your computer and use it in GitHub Desktop.
mysql node
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'mydb.com',
user:'myUser',
password: 'myPassword',
connectionLimit: 5
});
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
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);
});
});
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);
});
});
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