-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node js crud with mysql intergration
- Loading branch information
1 parent
80b0cca
commit f706b71
Showing
994 changed files
with
125,873 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
const PORT = 3000; | ||
|
||
const mysql = require('mysql2'); | ||
|
||
const db = mysql.createConnection({ | ||
host: 'localhost', | ||
user: 'root', | ||
password: '1234', | ||
database: 'testnode' | ||
}); | ||
|
||
db.connect(err => { | ||
if (err) { | ||
console.error('Error connecting to the database:', err.stack); | ||
return; | ||
} | ||
console.log('Connected to MySQL database.'); | ||
}); | ||
|
||
|
||
app.use(express.json()); // Middleware to parse JSON bodies | ||
|
||
app.get('/', (req, res) => { | ||
res.send('Hello, World!'); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server is running on http://localhost:${PORT}`); | ||
}); | ||
|
||
// CREATE: POST /items | ||
app.post('/items', (req, res) => { | ||
const { name } = req.body; | ||
const query = 'INSERT INTO items (name) VALUES (?)'; | ||
|
||
db.query(query, [name], (err, results) => { | ||
if (err) { | ||
return res.status(500).json({ error: err.message }); | ||
} | ||
res.status(201).json({ id: results.insertId, name }); | ||
}); | ||
}); | ||
|
||
// READ: GET /items | ||
app.get('/items', (req, res) => { | ||
const query = 'SELECT * FROM items'; | ||
db.query(query, (err, results) => { | ||
if (err) { | ||
return res.status(500).json({ error: err.message }); | ||
} | ||
res.json(results); | ||
}); | ||
}); | ||
|
||
// READ (Single): GET /items/:id | ||
app.get('/items/:id', (req, res) => { | ||
const query = 'SELECT * FROM items WHERE id = ?'; | ||
db.query(query, [req.params.id], (err, results) => { | ||
if (err) { | ||
return res.status(500).json({ error: err.message }); | ||
} | ||
if (results.length === 0) return res.status(404).send('Item not found'); | ||
res.json(results[0]); | ||
}); | ||
}); | ||
|
||
// UPDATE: PUT /items/:id | ||
app.put('/items/:id', (req, res) => { | ||
const { name } = req.body; | ||
const query = 'UPDATE items SET name = ? WHERE id = ?'; | ||
|
||
db.query(query, [name, req.params.id], (err, results) => { | ||
if (err) { | ||
return res.status(500).json({ error: err.message }); | ||
} | ||
if (results.affectedRows === 0) return res.status(404).send('Item not found'); | ||
res.json({ id: req.params.id, name }); | ||
}); | ||
}); | ||
|
||
// DELETE: DELETE /items/:id | ||
app.delete('/items/:id', (req, res) => { | ||
const query = 'DELETE FROM items WHERE id = ?'; | ||
|
||
db.query(query, [req.params.id], (err, results) => { | ||
if (err) { | ||
return res.status(500).json({ error: err.message }); | ||
} | ||
if (results.affectedRows === 0) return res.status(404).send('Item not found'); | ||
res.status(204).send(); // No content to send back | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.