SHARE
TWEET

Untitled

a guest Oct 4th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Strored Procedure*/
  2. /*
  3. CREATE PROCEDURE 'GetAllData' ()
  4. BEGIN
  5.     SELECT * FROM Employee;
  6. END$$
  7. */
  8.  
  9. /*
  10. * Nodejs Code
  11. */
  12. var express = require('express'),
  13.     mysql = require('mysql'),
  14.     bodyParser = require('body-parser'),
  15.     app = express();
  16.  
  17. app.use(bodyParser.json());
  18.  
  19. var port = 3000;
  20. var database = mysql.createPool({
  21.   connectionLimit : 100,
  22.   host: 'localhost',
  23.   user: '***',
  24.   password: '***',
  25.   database: 'dbName',
  26.   port: 3306
  27. });
  28.  
  29. app.get('/data',function(req,res){
  30.  
  31.     var appData = {};
  32.     let sql = 'CALL GetAllData()';
  33.  
  34.     database.getConnection(function(err,connection){
  35.         if(err){
  36.             appData['error'] = 1;
  37.             appData['data'] = 'Internal server error';
  38.             res.status(500).json(appData);
  39.         }else{
  40.             connection.query(sql,function(err,rows,fields){
  41.                 if(err){
  42.                     appData['error'] = 1;
  43.                     appData['data'] = 'Error running stored procedure';
  44.                     res.status(500).json(appData);
  45.                 }
  46.                 appData['error'] = 1;
  47.                 appData['data'] = rows;
  48.                 res.status(200).json(appData);
  49.             });
  50.             connection.release();
  51.         }
  52.     });
  53. });
  54.  
  55. app.listen(port,function(){
  56.     console.log('Server running on port',port);
  57. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top