Before You start first go through this link for setting your Node.js API environment.
Here I will show you how to retrieve your record using MySQL in node.js server. Follow this below procedure.
Write Select Query in Controller:
Here I will show you how to retrieve your record using MySQL in node.js server. Follow this below procedure.
Write Select Query in Controller:
For getting all records from MySQL table write query in “employeecontroller.js” file in “controller” folder.
In the Phpmyadmin page, the table structure looks like this:
MySQL query for getting all record is :
Select * from tbl_profile
Now use that query in the “employeecontroller.js” file in the “controller” folder.
Employeecontroller.js
In the Phpmyadmin page, the table structure looks like this:
MySQL query for getting all record is :
Select * from tbl_profile
Now use that query in the “employeecontroller.js” file in the “controller” folder.
Employeecontroller.js
var db = require('../config/connection'); //reference of connection.js
var employees = {
getAll: function (callback) {
return db.query("SELECT * FROM tbl_profile", callback);
}
}
module.exports=employees;
Route Configuration for get employee records:
For configuring route for getting all records from employee profile table. Write route configuration code in “employeeroute.js” file in “route” folder.
Set Get method to get all employee’s profile details. Now use this following code in your “employeeroute.js” file in the “route” folder.
employeeroute.js:
var express = require('express');
var router = express.Router();
var employees = require('../controller/employeecontroller'); //Call your employee controller...
router.get('/getall', function (req, res, next) {
employees.getAll(function (err, rows) {
if (err) {
res.send(err);
}
else {
res.send(rows);
}
})
})
module.exports = router;
var employees = {
getAll: function (callback) {
return db.query("SELECT * FROM tbl_profile", callback);
}
}
module.exports=employees;
Route Configuration for get employee records:
For configuring route for getting all records from employee profile table. Write route configuration code in “employeeroute.js” file in “route” folder.
Set Get method to get all employee’s profile details. Now use this following code in your “employeeroute.js” file in the “route” folder.
employeeroute.js:
var express = require('express');
var router = express.Router();
var employees = require('../controller/employeecontroller'); //Call your employee controller...
router.get('/getall', function (req, res, next) {
employees.getAll(function (err, rows) {
if (err) {
res.send(err);
}
else {
res.send(rows);
}
})
})
module.exports = router;
0 Comments