-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
79 lines (68 loc) · 1.77 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//aurthor : farrukh askari
//help was taaken from blog at codeforgeek.com
//code taken and modified from github account : codeforgeek
//twitter : @codeforgeek
// Update: 01 Jan 2016
var express = require("express");
var mysql = require("mysql");
var bodyParser = require("body-parser");
var md5 = require('MD5');
var rest = require("./REST.js");
var app = express();
var url = require('url');
require('dotenv').config();
function REST()
{
var self = this;
self.connectMysql();
};
REST.prototype.connectMysql = function()
{
var self = this;
var pool = mysql.createPool(
{
connectionLimit : 100,
host : process.env.host,
port : process.env.dbPort,
user : process.env.user,
password : process.env.password,
database : process.env.database,
debug : false
});
pool.getConnection(function(err,connection)
{
if(err)
{
self.stop(err);
}
else
{
self.configureExpress(connection);
}
});
};
REST.prototype.configureExpress = function(connection)
{
var self = this;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var router = express.Router();
app.use(express.static(__dirname + '/public'));
app.use('/public/index.html', router);
app.use('/api', router);
var rest_router = new rest(router, connection, md5);
self.startServer();
};
REST.prototype.startServer = function()
{
app.listen(process.env.port, function()
{
console.log("All right ! I am alive at Port " + process.env.port);
});
};
REST.prototype.stop = function(err)
{
console.log("ISSUE WITH MYSQL \n" + err);
process.exit(1);
};
new REST();