Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // call the packages we need
- var express = require('express');
- var app = express();
- var app2 = express();
- var fs = require('fs');
- var Promise = require('promise');
- var mysql = require("mysql");
- function allowCrossDomain(res) {
- res.setHeader('Access-Control-Allow-Origin', '*');
- res.setHeader('Access-Control-Allow-Methods', 'GET');
- }
- /*
- * Create the database connection
- */
- var con = mysql.createConnection({
- host: "localhost",
- user: "root",
- password: "иддш",
- database: "phones"
- });
- /*
- * Connect to the database
- */
- function connect() {
- con.connect(function(err) {
- if(err) {
- console.log('Error connecting to databse');
- return;
- }
- console.log('Connection successful');
- });
- }
- function disconnect() {
- con.end(function(err) {
- // The connection is terminated gracefully
- // Ensures all previously enqueued queries are still
- // before sending a COM_QUIT packet to the MySQL server.
- });
- }
- var port = process.env.PORT || 1234;
- var router = express.Router();
- /*
- * Gets an employee by id
- */
- router.get('/label/:id', function (req, res) {
- allowCrossDomain(res);
- connect();
- con.query('SELECT * FROM phoneLabel WHERE phoneLabel.id = ' + req.params.id, function(err, rows) {
- if(err) {
- res.json(err);
- } else {
- res.json(rows);
- }
- console.log('Data received from database:\n');
- console.log(rows);
- });
- // disconnect();
- });
- router.get('/labels', function (req, res) {
- allowCrossDomain(res);
- connect();
- con.query('SELECT * FROM phoneLabel', function(err, rows) {
- if(err) {
- res.json(err);
- } else {
- res.json(rows);
- }
- console.log('Data received from database:\n');
- console.log(rows);
- });
- });
- router.get('/label/model', function(req, res) {
- allowCrossDomain(res);
- connect();
- con.query('SELECT * FROM phoneLabel INNER JOIN phoneModel ON phoneModel.labelId = phoneLabel.id', function(err, rows) {
- if(err) {
- res.json(err);
- } else {
- res.json(rows);
- }
- console.log('Data received from database:\n');
- console.log(rows);
- });
- });
- router.get('/label/filter?', function(req, res) {
- allowCrossDomain(res);
- connect();
- con.query('SELECT * FROM phoneDetails INNER JOIN phoneModel ON phoneModel.id = phoneDetails.modelId INNER JOIN phoneLabel ON phoneLabel.id = phoneModel.labelId WHERE phoneDetails.', function(err, rows) {
- if(err) {
- res.json(err);
- } else {
- res.json(rows);
- }
- console.log('Data received from database:\n');
- console.log(rows);
- });
- });
- router.put('/add', function (req, res) {
- allowCrossDomain(res);
- // connect();
- // con.query('SELECT * FROM phoneLabel', function(err, rows) {
- // if(err) {
- // res.json(err);
- // } else {
- // res.json(rows);
- // }
- // console.log('Data received from database:\n');
- // console.log(rows);
- // });
- console.log(res);
- // disconnect();
- });
- router.delete('/label/:id', function (req, res) {
- allowCrossDomain(res);
- connect();
- con.query('DELETE FROM phoneLabel WHERE phoneLabel.id = ' + req.params.id, function(err, rows) {
- if(err) {
- res.json(err);
- } else {
- res.json(rows);
- }
- console.log('Data received from database:\n');
- console.log(rows);
- });
- // disconnect();
- });
- /*
- router.get('/total/take_home', function (req, res) {
- getTotalFrom('takeHome', res);
- });*/
- /*
- * Gets total annual income tax
- */
- router.get('/total/income_tax', function (req, res) {
- getTotalFrom('incomeTax', res);
- });
- /*
- * Gets national insurance
- */
- router.get('/total/national_insurance', function (req, res) {
- getTotalFrom('nationalInsurance', res);
- });
- app.use('/', router);
- app.listen(port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement