-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (55 loc) · 2.4 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const ejsLint = require('ejs-lint');
// loads the config files
const config = require("./config.json");
const dataconfig = require("./dbconfig.json");
//definds the infomation for the database
var dbConfig = {
host: dataconfig.mysqlhost, // ip address of server running mysql
user: dataconfig.mysqluser, // user name to your mysql database
password: dataconfig.mysqlpass, // corresponding password
database: dataconfig.mysqldata, // use the specified database
port: dataconfig.mysqlport
};
var mysqlcon;
function handleDisconnect() {
mysqlcon = mysql.createConnection(dbConfig); // Recreate the connection,
mysqlcon.connect(function onConnect(err) { // The server is either down
if (err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 10000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
mysqlcon.on('error', function onError(err) {
console.log('db error', err);
if (err.code == 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
// Create a new instance of express
const app = express()
// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.set('view engine', 'ejs');
app.get('/' , function (req, res){
res.render('\index');
})
app.get('/test' , function (req, res) {
console.log('test')
res.send('test')
});
//defines the port
app.listen(config.port, function (err) {
if (err) {
throw err
}
console.log(`Server started on port ${config.port}`)
});