Advertisement
jeffjeffjeff

ja-server.js

May 15th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require("express");
  2. const mysql = require("mysql");
  3.  
  4. // Creat the connection to the database
  5. const db = mysql.createConnection({
  6.   host: "localhost",
  7.   user: "root",
  8.   password: "password",
  9.   database: "tester"
  10. });
  11.  
  12. // connect and error check
  13. db.connect(err => {
  14.   if (err) {
  15.     throw err;
  16.   }
  17.   console.log("MySQL connected properly");
  18. });
  19.  
  20. // initiates an instance of express
  21. const app = express();
  22.  
  23. app.get("/api/tester", (req, res) => {
  24.   var items = [];
  25.   var sql = "SELECT * FROM tester";
  26.   var query = db.query(sql, function(err, rows) {
  27.     if (err) throw err;
  28.     res.json(rows);
  29.   });
  30. });
  31.  
  32. // TRYING TO FIGURE OUT THE DATABASE
  33.  
  34. const port = 5000;
  35.  
  36. app.listen(port, () => `Server running on port ${port}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement