Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. //server.js
  2. // MEAN Stack RESTful API Tutorial - Contact List App
  3.  
  4. var express = require('express');
  5. var app = express();
  6. var mysql = require('mysql');
  7. var bodyParser = require('body-parser');
  8.  
  9. var DBconnectionString = {
  10. host: '107.170.169.80',
  11. user: 'david',
  12. password: 'david!102',
  13. database: 'NCD'
  14. };
  15.  
  16. app.use(express.static(__dirname + '/public'));
  17. app.use(bodyParser.json());
  18.  
  19. app.get('/degreelist', function(req, res){
  20. console.log('I received a GET request');
  21.  
  22. var connection = mysql.createConnection(DBconnectionString);
  23. connection.connect();
  24. connection.query('SELECT DegreeID, Description FROM DEGREE WHERE DegreeID > 0', function (err, docs) {
  25. // console.log(docs);
  26. res.json(docs);
  27. });
  28. connection.end();
  29.  
  30. });
  31.  
  32. app.get('/degreelist/:id', function (req, res) {
  33. var id = req.params.id;
  34.  
  35. var connection = mysql.createConnection(DBconnectionString);
  36. console.log(id);
  37. connection.connect();
  38. connection.query('SELECT * FROM UNIVERSE where DegreeID = ' + id, function (err, doc) {
  39. console.log(doc);
  40. res.json(doc);
  41. });
  42. connection.end();
  43. });
  44.  
  45. // app.get('/contactlist', function (req, res) {
  46. // console.log('I received a GET request');
  47. // var connection = mysql.createConnection({
  48. // host: '107.170.169.80',
  49. // user: 'david',
  50. // password: 'david!102',
  51. // });
  52.  
  53. // connection.connect;
  54. // connection.query('SELECT * FROM NCV.contactlist', function (err, docs) {
  55. // console.log(docs);
  56. // res.json(docs);
  57. // });
  58. // connection.end();
  59. // });
  60.  
  61. // app.post('/contactlist', function (req, res) {
  62. // console.log(req.body);
  63. // var connection = mysql.createConnection({
  64. // host: '107.170.169.80',
  65. // user: 'david',
  66. // password: 'david!102',
  67. // });
  68.  
  69. // connection.connect;
  70. // connection.query('Insert INTO NCV.contactlist SET ?', req.body , function (err, doc) {
  71. // res.json(doc);
  72. // });
  73. // connection.end();
  74. // });
  75.  
  76. // app.delete('/contactlist/:id', function (req, res) {
  77. // var id = req.params.id;
  78. // var connection = mysql.createConnection({
  79. // host: '107.170.169.80',
  80. // user: 'david',
  81. // password: 'david!102',
  82. // });
  83.  
  84. // console.log(id);
  85. // connection.connect;
  86. // connection.query('DELETE FROM NCV.contactlist where id = ' + id, function (err, doc) {
  87. // res.json(doc);
  88. // });
  89. // connection.end();
  90. // });
  91.  
  92. // app.get('/contactlist/:id', function (req, res) {
  93. // var id = req.params.id;
  94. // var connection = mysql.createConnection({
  95. // host: '107.170.169.80',
  96. // user: 'david',
  97. // password: 'david!102',
  98. // });
  99. // console.log(id);
  100. // connection.connect;
  101. // connection.query('SELECT * FROM NCV.contactlist where id = ' + id, function (err, doc) {
  102. // console.log(doc[0]);
  103. // res.json(doc[0]);
  104. // });
  105. // connection.end();
  106. // });
  107.  
  108. // app.put('/contactlist/:id', function (req, res) {
  109. // var id = req.params.id;
  110. // var connection = mysql.createConnection({
  111. // host: '107.170.169.80',
  112. // user: 'david',
  113. // password: 'david!102',
  114. // });
  115. // console.log(req.body.name);
  116.  
  117. // connection.connect;
  118. // connection.query('UPDATE NCV.contactlist SET ? WHERE id = ' + id, req.body,function (err, doc) {
  119. // console.log(doc);
  120. // res.json(doc);
  121. // });
  122.  
  123. // connection.end();
  124. // });
  125.  
  126. app.listen(3000);
  127. console.log("Server running on port 3000");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement