-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (59 loc) · 2.06 KB
/
index.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
// Import dependencys
const express = require('express');
const path = require('path');
const fs = require('fs');
// Import Json files
const config = require('./config.json')
const svr = require('./svr/svr.json')
// Express
const app = express();
const port = process.env.PORT || 8080;
fs.readFile('./config.json', 'utf8', (err, jsonString) => {
if (err) {
console.log("File read failed:", err)
return
}
try {
const config = JSON.parse(jsonString)
console.log("User name is:", config.user)
} catch (err) {
console.log('Error parsing JSON string:', err)
}
// Get link pages
try {
if (config.symbol == "" || config.symbol == null) {
app.get('/' + config.home, function(req, res) {
res.sendFile(path.join(__dirname, 'views/link.html'));
});
console.log("We could not find a symbol!")
console.log("Link has been set to: http://localhost:" + port + "/" + config.home);
} else {
app.get('/'+ config.symbol + '/' + config.home, function(req, res) {
res.sendFile(path.join(__dirname, 'views/link.html'));
});
console.log("We found a symbol! (" + config.symbol +")")
console.log("Link is listing at http://localhost:" + port + "/" + config.symbol + '/' + config.home);
}
} catch (err) {
console.log("We tried to list your links but we have encountered an error. Please check /config.json for more!", err)
}
})
// Get static files
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'views/index.html'));
});
// Allow client-side javascript:
try {
app.use(express.static('stat'));
app.get('/config.json', function(req, res) {
res.sendFile(path.join(__dirname, '/config.json'));
});
console.log("Static files have been loaded & published")
} catch (err) {
console.log("We have encountered an error loading client-side Javascript. A reatart of the app is sugested")
console.log(err)
}
// Listen for app
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});