npm create-react-app node-mariadb-react
console
npm init -y
npm i express mysql mariadb nodemon
index.js생성
// import express from 'express'
const express = require('express')
const app = express()
app.listen(9900, ()=>{
console.log("Connected to backend!!333");
})
package.json에 start 추가
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
}
}
npx create-react-app .
npm i react-router-dom
교재 600p
// insert
app.post("/books", (req, res)=>{
const sql = "insert into books(`title`,`description`,`price`,`cover`) values (?)";
const values = [
req.body.title,
req.body.description,
req.body.price,
req.body.cover
];
[
{
"title": "aaaa",
"description": "asdasasas",
"price": 30000,
"cover": "cover test"
}
]
db.query(sql,[values], (err, data)=>{
if(err) return res.json(err)
return res.json("Books has been create successfuly")
})
})
// delete
app.delete("/books/:id", (req, res) => {
let id = req.params.id;
const sql = "DELETE FROM books WHERE id= ?";
console.log("id: ", req.params.id);
db.query(sql, [id], (err, data) => {
if(err) return res.json(err)
return res.json("Books has been delete successfuly")
});
});