Guest User

Untitled

a guest
Jan 7th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. const mysql = require('mysql');
  2. const express = require('express');
  3. const app = express();
  4.  
  5. var con = mysql.createConnection({
  6. host: "52.208.53.100",
  7. user: "ssostag",
  8. password: "sskDwm8X",
  9. database: "test"
  10. });
  11.  
  12. con.connect(function(err) {
  13. if(err) throw err;
  14. console.log("Connected!");
  15.  
  16. app.use(express.json());
  17. app.use(express.urlencoded({
  18. extended: true
  19. }));
  20.  
  21. app.get('/', function(req, res) {
  22. res.send({
  23. "api-version": "1.0.0"
  24. });
  25. });
  26.  
  27. app.get('/customers', function (req, res)
  28. {
  29. con.query("SELECT * FROM customer", function (err, result, fields) {
  30. // if (err) throw err;
  31. res.send(result)
  32. });
  33. });
  34.  
  35.  
  36. app.get('/customers/:id', function(req, res) {
  37.  
  38. con.query("SELECT * FROM customer WHERE id = ?", req.params.id, function(err, result, fields)
  39. {
  40. // if (err) throw err;
  41. res.send(result)
  42. });
  43. });
  44.  
  45. app.post('/customers', function(req, res)
  46. {
  47. var customer = {
  48. name: req.body.name
  49. }
  50. con.query("INSERT INTO customer SET ?", customer, function(err, result)
  51. {
  52. // if (err) throw err;
  53. console.log(err);
  54. res.send(customer)
  55. });
  56. })
  57.  
  58. app.put('/customers/:id', function(req, res) {
  59.  
  60. var customer = {
  61. name: req.body.name
  62. }
  63. con.query("UPDATE customer SET ? WHERE id = ?", customer, req.params.id, function(err, result)
  64. {
  65. // if (err) throw err;
  66. res.send(customer)
  67. });
  68. })
  69.  
  70. app.listen(3000);
  71. app.on("end", () => {
  72. client.close();
  73. });
  74. });
Add Comment
Please, Sign In to add comment