In this video we'll see:
Learn Node JS with MySQL APIs Development Beginners Tutorial | Update Data API PUT Method
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const PORT = 5000;
// include body parser for body parameters
app.use(bodyParser.json());
// load mysql package
const mysql = require("mysql");
// create mysql connection
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "node_db"
});
// check connection
connection.connect(function (error) {
if (error) {
throw error;
} else {
console.log("We are now successfully connected with mysql database");
}
});
// get all data api
app.get("/users", function (request, response) {
// query
connection.query("SELECT * from tbl_users", function(error, results, fields){
if(error){
throw error;
}else{
response.json(results);
//console.log(fields);
}
});
});
// insert data api method - post
app.post("/user", function(request, response){
//console.log(request.body);return false;
//var name = request.body.name;
//var email = request.body.email;
//var phone_no = request.body.phone_no;
//connection.query("INSERT INTO tbl_users (name, email, phone_no) values (?, ?, ?)", [name, email, phone_no], function(error, result, fields){
var params = request.body;
connection.query("INSERT INTO tbl_users SET ?", params, function(error, result, fields){
response.json({
status: 1,
message: "Data inserted successfully",
data: result,
insert_id: result.insertId
});
});
});
// delete data
app.delete("/user", function(request, response){
var user_id = request.body.user_id;
connection.query("DELETE from tbl_users WHERE id = ?", [user_id], function(error, result, fields){
if(error){
throw error;
}else{
response.json({
status : 1,
message: "User has been deleted successfully",
data: result
});
}
});
});
// update data
app.put("/user", function (request, response) {
var params = request.body;
var name = params.name;
var email = params.email;
var phone_no = params.phone_no;
var user_id = params.user_id;
connection.query("Update tbl_users SET name = ?, email = ?, phone_no = ? WHERE id = ?", [name, email, phone_no, user_id], function (error, results, fields) {
if (error) {
throw error;
} else {
response.json({
status: 1,
message: "User has been updated successfully"
});
}
});
});
app.get("/", function (request, response) {
response.send("Welcome to Initial stage of MySQL");
});
app.listen(PORT, function () {
console.log("Server is running at 5000 port");
});
SOCIAL :
===============
Subscribe : / owthub
FACEBOOK : / owthub
TWITTER: / owthub
BLOG: https://onlinewebtutorhub.blogspot.in/
UDEMY: https://www.udemy.com/user/online-web...
Other Tutorials
===============
Wordpress Customizations:
---------------------------------
Wordpress Theme (Hindi): https://goo.gl/MZ8maF
Wordpress Widget (Hindi): https://goo.gl/Dc2htn
Wordpress Plugin (English): https://goo.gl/BA7dYG
Wordpress Theme Options (English): https://goo.gl/Vjv3Ub
Wordpress JSON Rest API (English): https://goo.gl/SVQRQR
Wordpress JSON Rest API (Hindi): https://goo.gl/NNWfKa
and many more...
Javascript framework:
----------------------------------
Learn backbone.js here! (English) : https://goo.gl/Qd2Pcs
Learn Vue JS here ! (Hindi): https://goo.gl/MVtsmh
PHP Frameworks:
----------------------------------
Laravel tutorial (Hindi): https://goo.gl/Nh9qJk
CakePHP tutorial (Hindi): https://goo.gl/uRsS3G
Tags:
----------------------------------
node,
node.js,
node api,
node with express apis,
node express with mysql apis,
node mysql,
mysql node apis,
node mysql api tutorial,
node restful api tutorial with mysql,
express mysql node api,
node express apis,
node restful apis with mysql,
learn node js with mysql,
node js express and mysql,
restful node apis development tutorial,
node js apis development tutorial from scratch,
node express tutorial,
create restful api nodejs mysql,
online web tutor,
online web tutor node js express mysql,
online web tutor node restful apis development,
Thanks
Online Web Tutor
Keep learning and Sharing :)