Advertisement
Guest User

Untitled

a guest
Oct 7th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. [{"id":1,"name":"Rita","cellphone":"93999292","provider":{"name":"AT&T, "code":15}}]
  2.  
  3. var express = require("express");
  4. var mysql = require("mysql");
  5. var app = express();
  6.  
  7. var pool = mysql.createPool({
  8. connetionLimit: 100,
  9. host: "localhost",
  10. user: "root",
  11. password: "toor",
  12. database: "contacts",
  13. debug: "false"
  14. });
  15.  
  16. function handle_database(request, response, query) {
  17. pool.getConnection(function (err, connection) {
  18. if (err) {
  19. response.json({"code": 100, "status": "Error connecting to the database."});
  20. return;
  21. }
  22.  
  23. connection.query(query, function (err, rows) {
  24. connection.release();
  25.  
  26. if (!err) {
  27. response.header("Access-Control-Allow-Origin", "*");
  28. response.json(rows);
  29. }
  30. });
  31.  
  32. connection.on("error", function (err) {
  33. response.json({"code": 100, "status": "Error connecting to the database."});
  34. return;
  35. });
  36. });
  37. }
  38.  
  39. app.get("/", function (request, response) {
  40. response.send("Default");
  41. });
  42.  
  43. app.get("/listContacts", function (request, response) {
  44. var _query = "select * from contact";
  45. handle_database(request, response, _query);
  46. });
  47.  
  48. app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement