├── package.json ├── README.md └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "project", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.18.2", 13 | "express": "^4.16.3", 14 | "mysql": "^2.15.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node.js + MySQL CRUD Operation 2 | Content Discussed : 3 | - How to Connect MySQL DB from Node.js 4 | - Execute MySQL Query From Node.js 5 | - Use of MySQL Store Procedure in Node.js. 6 | 7 | 8 | ## Get the Code 9 | 10 | ``` 11 | $ git clone https://github.com/CodAffection/Node.js-MySQL-CRUD-Operations.git 12 | $ cd Node.js-MySQL-CRUD-Operations 13 | $ npm install 14 | //update MySQL DB configuration according to yours. 15 | ``` 16 | 17 | ## How it works ? 18 | 19 | :tv: Video tutorial on this same topic 20 | Url : https://youtu.be/4fWWn2Pe2Mk 21 | 22 | Video Tutorial for Node.JS + MySQL 25 | 26 | 27 | | :bar_chart: | List of Tutorials | | :moneybag: | Support Us | 28 | |--------------------------:|:---------------------|---|---------------------:|:-------------------------------------| 29 | | Angular |http://bit.ly/2KQN9xF | |Paypal | https://goo.gl/bPcyXW | 30 | | Asp.Net Core |http://bit.ly/30fPDMg | |Amazon Affiliate | https://geni.us/JDzpE | 31 | | React |http://bit.ly/325temF | | 32 | | Python |http://bit.ly/2ws4utg | | :point_right: | Follow Us | 33 | | Node.js |https://goo.gl/viJcFs | |Website |http://www.codaffection.com | 34 | | Asp.Net MVC |https://goo.gl/gvjUJ7 | |YouTube |https://www.youtube.com/codaffection | 35 | | Flutter |https://bit.ly/3ggmmJz| |Facebook |https://www.facebook.com/codaffection | 36 | | Web API |https://goo.gl/itVayJ | |Twitter |https://twitter.com/CodAffection | 37 | | MEAN Stack |https://goo.gl/YJPPAH | | 38 | | C# Tutorial |https://goo.gl/s1zJxo | | 39 | | Asp.Net WebForm |https://goo.gl/GXC2aJ | | 40 | | C# WinForm |https://goo.gl/vHS9Hd | | 41 | | MS SQL |https://goo.gl/MLYS9e | | 42 | | Crystal Report |https://goo.gl/5Vou7t | | 43 | | CG Exercises in C Program |https://goo.gl/qEWJCs | | 44 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const mysql = require('mysql'); 2 | const express = require('express'); 3 | var app = express(); 4 | const bodyparser = require('body-parser'); 5 | 6 | app.use(bodyparser.json()); 7 | 8 | var mysqlConnection = mysql.createConnection({ 9 | host: 'localhost', 10 | user: 'root', 11 | password: '1234', 12 | database: 'EmployeeDB', 13 | multipleStatements: true 14 | }); 15 | 16 | mysqlConnection.connect((err) => { 17 | if (!err) 18 | console.log('DB connection succeded.'); 19 | else 20 | console.log('DB connection failed \n Error : ' + JSON.stringify(err, undefined, 2)); 21 | }); 22 | 23 | 24 | app.listen(3000, () => console.log('Express server is runnig at port no : 3000')); 25 | 26 | 27 | //Get all employees 28 | app.get('/employees', (req, res) => { 29 | mysqlConnection.query('SELECT * FROM Employee', (err, rows, fields) => { 30 | if (!err) 31 | res.send(rows); 32 | else 33 | console.log(err); 34 | }) 35 | }); 36 | 37 | //Get an employees 38 | app.get('/employees/:id', (req, res) => { 39 | mysqlConnection.query('SELECT * FROM Employee WHERE EmpID = ?', [req.params.id], (err, rows, fields) => { 40 | if (!err) 41 | res.send(rows); 42 | else 43 | console.log(err); 44 | }) 45 | }); 46 | 47 | //Delete an employees 48 | app.delete('/employees/:id', (req, res) => { 49 | mysqlConnection.query('DELETE FROM Employee WHERE EmpID = ?', [req.params.id], (err, rows, fields) => { 50 | if (!err) 51 | res.send('Deleted successfully.'); 52 | else 53 | console.log(err); 54 | }) 55 | }); 56 | 57 | //Insert an employees 58 | app.post('/employees', (req, res) => { 59 | let emp = req.body; 60 | var sql = "SET @EmpID = ?;SET @Name = ?;SET @EmpCode = ?;SET @Salary = ?; \ 61 | CALL EmployeeAddOrEdit(@EmpID,@Name,@EmpCode,@Salary);"; 62 | mysqlConnection.query(sql, [emp.EmpID, emp.Name, emp.EmpCode, emp.Salary], (err, rows, fields) => { 63 | if (!err) 64 | rows.forEach(element => { 65 | if(element.constructor == Array) 66 | res.send('Inserted employee id : '+element[0].EmpID); 67 | }); 68 | else 69 | console.log(err); 70 | }) 71 | }); 72 | 73 | //Update an employees 74 | app.put('/employees', (req, res) => { 75 | let emp = req.body; 76 | var sql = "SET @EmpID = ?;SET @Name = ?;SET @EmpCode = ?;SET @Salary = ?; \ 77 | CALL EmployeeAddOrEdit(@EmpID,@Name,@EmpCode,@Salary);"; 78 | mysqlConnection.query(sql, [emp.EmpID, emp.Name, emp.EmpCode, emp.Salary], (err, rows, fields) => { 79 | if (!err) 80 | res.send('Updated successfully'); 81 | else 82 | console.log(err); 83 | }) 84 | }); 85 | --------------------------------------------------------------------------------