Skip to content

es6 javascript #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ I have created simple example to create RESTFul api for crud operation using mys
<li>Add Record using rest call into mysql database</li>
<li>Edit Record using rest call into mysql database</li>
<li>Delete Record using rest call into mysql database</li>
<li>Featch records using rest call into mysql database</li>
<li>Fetch records using rest call into mysql database</li>
</ul>

# how to create table in db
You need to craete 'test' database into mysql and import customer.sql table into your mysql database.
You need to create 'test' database into mysql and import customer.sql table into your mysql database.

# How to run nodejs application
<p>copy index.js and package.json file into your nodejs project folder,</p>
Expand Down
79 changes: 79 additions & 0 deletions es6 version/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const http = require('http');
const express = require('express');
let app = express();
const mysql = require('mysql');
const bodyParser = require('body-parser');

let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'test'
});

connection.connect((err) => {
if (err) throw err(
console.log("You have an error in connection"));
else {
console.log("No error")

}
});

app.use(bodyParser.json());
app.use((bodyParser.urlencoded)({
extended: true
}));


const server = app.listen(3000, "127.0.0.1", () => {
const host = server.address().address;
const port = server.address().port;
console.log("App listening to", host, port)
});


// rest api to get all customers
app.get('/customer', (req, res) => {
connection.query('select * from customer', (error, results, fields) => {
if (error) throw error;
res.send(JSON.stringify(results))
})
});


//rest api to get a single customer data
app.get('/customer/:id', (req, res) => {
connection.query('select * from customer where Id=?', [req.params.id], (error, results, fields) => {
if (error) throw error;
res.end(JSON.stringify(results));

})
});

//rest api to create a new customer record into mysql database
app.post('/customer', (req, res) => {
let params = req.body;
console.log(params);
connection.query('INSERT INTO customer SET ?', params, function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
});
});

//rest api to update record into mysql database
app.put('/customer', (req, res) => {
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) {
if (error) throw error;
res.end(JSON.stringify(results));
});
});

//rest api to delete record from mysql database
app.delete('/customer', (req, res) => {
console.log(req.body);
connection.query('DELETE FROM `customer` WHERE `Id`=?', [req.body.Id], (error, results, fields) => {
if (error) throw error;
res.end('Record has been deleted!');
});
});
Loading