const mysql = require('mysql');
const express = require('express');
var app = express();
const bodyparser = require('body-parser');
app.use(bodyparser.json());
var mysqlConnection = mysql.createConnection({
host:'localhost',
user:'root',
database:'employeedb'
});
mysqlConnection.connect((err)=>{
if(!err)
console.log('DB Connection SuccessFull');
else
console.log('DB Connection Failed\n Error: '+JSON.stringify(err,undefined,2));
});
// Get All Employees
app.listen(3000, ()=>console.log('Express server is running at port No: 3000'));
app.get('/employees',(req,res)=>{
mysqlConnection.query('SELECT * FROM employees', (err, rows, fields)=>{
if(!err)
res.send(rows);
else
console.log(err);
})
});
// Get an Employees
app.get('/employees/:id',(req,res)=>{
mysqlConnection.query('SELECT * FROM employees WHERE id =?', [req.params.id],(err, rows, fields)=>{
if(!err)
res.send(rows);
else
console.log(err);
})
});
// Delete an Employees
app.delete('/employees/:id',(req,res)=>{
mysqlConnection.query('DELETE from employees WHERE id =?', [req.params.id],(err, rows, fields)=>{
if(!err)
res.send('Deleted SuccessFully');
else
console.log(err);
})
});
const express = require('express');
var app = express();
const bodyparser = require('body-parser');
app.use(bodyparser.json());
var mysqlConnection = mysql.createConnection({
host:'localhost',
user:'root',
database:'employeedb'
});
mysqlConnection.connect((err)=>{
if(!err)
console.log('DB Connection SuccessFull');
else
console.log('DB Connection Failed\n Error: '+JSON.stringify(err,undefined,2));
});
// Get All Employees
app.listen(3000, ()=>console.log('Express server is running at port No: 3000'));
app.get('/employees',(req,res)=>{
mysqlConnection.query('SELECT * FROM employees', (err, rows, fields)=>{
if(!err)
res.send(rows);
else
console.log(err);
})
});
// Get an Employees
app.get('/employees/:id',(req,res)=>{
mysqlConnection.query('SELECT * FROM employees WHERE id =?', [req.params.id],(err, rows, fields)=>{
if(!err)
res.send(rows);
else
console.log(err);
})
});
// Delete an Employees
app.delete('/employees/:id',(req,res)=>{
mysqlConnection.query('DELETE from employees WHERE id =?', [req.params.id],(err, rows, fields)=>{
if(!err)
res.send('Deleted SuccessFully');
else
console.log(err);
})
});
No comments:
Post a Comment