├── README.md ├── customer.sql ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 |

Nodejs Rest API Example Using ExpressJS and Mysql

2 | I have created simple example to create RESTFul api for crud operation using mysql database.I have added following features into this source code, 3 | 9 | 10 | # how to create table in db 11 | You need to craete 'test' database into mysql and import customer.sql table into your mysql database. 12 | 13 | # How to run nodejs application 14 |

copy index.js and package.json file into your nodejs project folder,

15 |

Open command line and cd to your above nodejs project folder

16 |

run 'npm install'

17 |

run 'node index.js'

18 | -------------------------------------------------------------------------------- /customer.sql: -------------------------------------------------------------------------------- 1 | -- phpMyAdmin SQL Dump 2 | -- version 4.1.14 3 | -- http://www.phpmyadmin.net 4 | -- 5 | -- Host: 127.0.0.1 6 | -- Generation Time: May 21, 2017 at 03:16 PM 7 | -- Server version: 5.6.17 8 | -- PHP Version: 5.5.12 9 | 10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 | SET time_zone = "+00:00"; 12 | 13 | 14 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 15 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 16 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 17 | /*!40101 SET NAMES utf8 */; 18 | 19 | -- 20 | -- Database: `test` 21 | -- 22 | 23 | -- -------------------------------------------------------- 24 | 25 | -- 26 | -- Table structure for table `customer` 27 | -- 28 | 29 | CREATE TABLE IF NOT EXISTS `customer` ( 30 | `Id` int(11) NOT NULL AUTO_INCREMENT, 31 | `Name` varchar(255) NOT NULL, 32 | `Address` varchar(255) NOT NULL, 33 | `Country` varchar(100) NOT NULL, 34 | `Phone` int(10) NOT NULL, 35 | `Created_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 36 | `Updated_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 37 | PRIMARY KEY (`Id`) 38 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 39 | 40 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 41 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 42 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var http = require("http"); 2 | var express = require('express'); 3 | var app = express(); 4 | var mysql = require('mysql'); 5 | var bodyParser = require('body-parser'); 6 | 7 | //start mysql connection 8 | var connection = mysql.createConnection({ 9 | host : 'localhost', //mysql database host name 10 | user : 'root', //mysql database user name 11 | password : '', //mysql database password 12 | database : 'test' //mysql database name 13 | }); 14 | 15 | connection.connect(function(err) { 16 | if (err) throw err 17 | console.log('You are now connected with mysql database...') 18 | }) 19 | //end mysql connection 20 | 21 | //start body-parser configuration 22 | app.use( bodyParser.json() ); // to support JSON-encoded bodies 23 | app.use(bodyParser.urlencoded({ // to support URL-encoded bodies 24 | extended: true 25 | })); 26 | //end body-parser configuration 27 | 28 | //create app server 29 | var server = app.listen(3000, "127.0.0.1", function () { 30 | 31 | var host = server.address().address 32 | var port = server.address().port 33 | 34 | console.log("Example app listening at http://%s:%s", host, port) 35 | 36 | }); 37 | 38 | //rest api to get all customers 39 | app.get('/customer', function (req, res) { 40 | connection.query('select * from customer', function (error, results, fields) { 41 | if (error) throw error; 42 | res.end(JSON.stringify(results)); 43 | }); 44 | }); 45 | //rest api to get a single customer data 46 | app.get('/customer/:id', function (req, res) { 47 | connection.query('select * from customers where Id=?', [req.params.id], function (error, results, fields) { 48 | if (error) throw error; 49 | res.end(JSON.stringify(results)); 50 | }); 51 | }); 52 | 53 | //rest api to create a new customer record into mysql database 54 | app.post('/customer', function (req, res) { 55 | var params = req.body; 56 | console.log(params); 57 | connection.query('INSERT INTO customer SET ?', params, function (error, results, fields) { 58 | if (error) throw error; 59 | res.end(JSON.stringify(results)); 60 | }); 61 | }); 62 | 63 | //rest api to update record into mysql database 64 | app.put('/customer', function (req, res) { 65 | connection.query('UPDATE `customer` SET `Name`=?,`Address`=?,`Country`=?,`Phone`=? where `Id`=?', [req.body.Name,req.body.Address, req.body.Country, req.body.Phone, req.body.Id], function (error, results, fields) { 66 | if (error) throw error; 67 | res.end(JSON.stringify(results)); 68 | }); 69 | }); 70 | 71 | //rest api to delete record from mysql database 72 | app.delete('/customer', function (req, res) { 73 | console.log(req.body); 74 | connection.query('DELETE FROM `customer` WHERE `Id`=?', [req.body.Id], function (error, results, fields) { 75 | if (error) throw error; 76 | res.end('Record has been deleted!'); 77 | }); 78 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-restapi", 3 | "version": "1.0.0", 4 | "description": "Simple Example of nodejs to create rest call using MySQL", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": { 10 | "name": "Rachel", 11 | "email": "restapi2example@gmail.com", 12 | "url": "http://www.resapiexample.com/" 13 | }, 14 | "license": "MIT", 15 | "dependencies": { 16 | "body-parser": "^1.16.1", 17 | "express": "^4.14.1", 18 | "mysql": "^2.13.0" 19 | } 20 | } --------------------------------------------------------------------------------