NodeJS RESTful CRUD API and MYSQL – node js Restify Tutorial
In this Post We Will Explain About is NodeJS RESTful CRUD API and MYSQL – node js Restify Tutorial With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Node JS REST API Example Using Restify and MySQLExample
In this post we will show you Best way to implement creating a rest api using node.js and mysql, hear for building rest api with node js and mysqlwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.
Rest API Using Nodejs and RESTful Web-Services with MySQL
We are using following some files as well as Directory
We are creating a Directory ‘nodejs-RESTful-restapi-example’.It’s is our nodejs web-application name.
package.json: It’s file would have lots of nodejs some dependencies module for this best example.
config.js: It’s file would use for databse connection some parameters as well as object for db like as a mysql.
main.js: It’s file would use to make nodejs web-application server side as well as routes url.
node_modules Directory: It’s Directory would contains lots of nodejs packages.
Make MYSQL Database and Table Structure for simple User Authentication
We are using db like as a mysql server for store lots of the info like as a user registration information into stored MySQL table.you would make ‘live24u’ name database as well as into MySQL server.you would make ‘students’ table into ‘live24u’ mysql database using below some source script.
CREATE TABLE IF NOT EXISTS `students` ( `studid` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key', `students_name` varchar(255) NOT NULL COMMENT 'students name', `students_salary` double NOT NULL COMMENT 'students salary', `students_age` int(11) NOT NULL COMMENT 'students age', PRIMARY KEY (`studid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=3 ;
Make Package.json file
you would make package.json file for this RESTful Web-Services nodejs simple application into root path of following node js application Like as a ‘nodejs-RESTful-restapi-example’.It’s file create a simple what external libraries or some modules would be use in simple this nodejs web-application.You need to create below some sourcecode into simple package.json file.
{ "name": "nodejs-restapi", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "live24u": "echo \"Error: no live24u specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "mysql": "^2.14.1", "RESTful": "^5.2.0" } }
d:/nodejs-RESTful-restapi-example> npm install
The Node js RESTful Web Services Information are as follows:
Route | Method | Type | Posted Data Json | Description |
---|---|---|---|---|
/studentss | GET | Data Json | – | Get lots of studentss data |
/studentss/{studid} | GET | Data Json | – | Get a single students data |
/studentss | POST | Data Json |
{"students_name": "Rachel", "students_age": "34", "students_salary" : "23421"} |
Insert new students data row(records) into database |
/studentss | PUT | Data Json |
{"students_name": "Rachel", "students_age": "34", "students_salary" : "23421", "studid":21} |
Update students data row(records) into database |
/studentss | DELETE | Data Json |
{"studid" : 59} |
Delete particular students data row(records) from database |
Make MySQL Connection with Nodejs
MySQL is very popular opensource simple relational Management database.you would make simple MySQL database simple connection data into config.js in your package file The config.js directory file location would Like as a be d:\nodejs-RESTful-restapi-example\config.js. you would simple add following source code into this file sample.
'use strict' var mysql = require('mysql'); module.exports = { name: 'RESTful-Web-Services', hostname : 'http://localhost', version: '0.0.1', env: process.env.NODE_ENV || 'development', port: process.env.PORT || 3000, db: { get : mysql.makeConnection({ host : 'localhost', user : 'root', password : '', database : 'live24u1' }) } }
Make Node.js Rest Api using Restify and MySQL
Phase 1: Maked main.js file into simple js frameworks node js project and maked simple dependency module steps instances.
const config = require('./config'), RESTful = require('RESTful'), mysql = require('mysql')
Phase 2: Get MySQL connection object tomain.js file.
var connection = config.db.get;
Phase 3: you would add some RESTful API plugin for json data convert means parser as well as configuration (settings)like following below,
//use nodejs server server.use(RESTful.plugins.acceptParser(server.acceptable)); server.use(RESTful.plugins.queryParser()); server.use(RESTful.plugins.bodyParser());
Make node.js Server
you would make node.js RESTful server that would some listen our request and response on particular port simple.We are running here node js server on used to 3001 port, you can some more change port as per your generally port availability.
/** * Pakainfo.com Initialize Server */ const server = RESTful.makeServer({ name : config.name, version : config.version, url : config.hostname }); server.listen(3001, function () { console.log('%s listening at %s', server.name, server.url); });
Node.js Rest Api to fetch lots of data row(records) from MySQL Database Using Restify
you would make a GET Rest Request to access lots of students data row(records)s from simple MySQL database each table.you would use MySQL SQL query to Retrive data from students table and send some json data to client simple as response each object.
//RESTful Web-Services to get lots of response server.get('/studentss', function (req, results) { connection.query('select * from students', function (error, response, fields) { if (error) throw error; results.end(JSON.stringify(response)); }); });
Rest Api to get single data row(records) from Mysql Database Using Node.js
you would make a GET type RESTful request to access a single students data row(records) from MySQL database table.you would use simple MySQL query to Retrive data row(records) of particular students and send json information data to client side as response per object.
//RESTful Web-Services to get a single students data server.get('/studentss/:studid', function (req, results) { connection.query('select * from students where studid=?', [req.params.studid], function (error, response, fields) { if (error) throw error; results.end(JSON.stringify(response)); }); });
Rest Api to Make New Record into MySQL Using Node.js and Restify
you would make a new Rest Api to make a new students data entry into MySQL database table using node.js server. We would make POST type Rest request so you would post some data JSON data to node server.
//RESTful Web-Services to make a new data row(records) into simple mysql database server.post('/studentss', function (req, results) { var postData = req.body; connection.query('INSERT INTO students SET ?', postData, function (error, response, fields) { if (error) throw error; results.end(JSON.stringify(response)); }); });
Rest Web-Services to Update Record into simple MySQL Database Using Node JS
you would make new PUT type Restful Web-Services request using js frameworks nodejs and RESTful to update data into simple MySQL database.you need to pass students studid which would use to updated data row(records) into table.
//RESTful Web-Services to update data row(records) into mysql database table server.put('/studentss', function (req, results) { connection.query('UPDATE `students` SET `students_name`=?,`students_salary`=?,`students_age`=? where `studid`=?', [req.body.students_name,req.body.students_salary, req.body.students_age, req.body.studid], function (error, response, fields) { if (error) throw error; results.end(JSON.stringify(response)); }); });
Node js RESTful Web Services to Delete Record from MySQL Database
you would make new DELETE Type RESTful Api request using node js to delete students data row(records) from MySQL database table.you would pass students studid as a parameters which we want to remove from MySQL database table.
//RESTful Web-Services to delete data row(records) from simple mysql database server.delete('/studentss/:studid', function (req, results) { connection.query('DELETE FROM `students` WHERE `studid`=?', [req.params.studid], function (error, response, fields) { if (error) throw error; results.end('Pakainfo.com - Record has been deleted!'); }); });
I hope you have Got What is Node JS REST API Example Using Restify and MySQL And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.