-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
46 lines (40 loc) · 1.29 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
var express = require("express");
var logger = require("morgan");
var chalk = require("chalk");
var path = require("path");
var fs = require('fs');
var https = require('https');
var credentials;
var ssl = false;
var device = require('express-device');
var compression = require('compression');
// SSL cert
var privateKey = fs.readFileSync('sslcert/key.pem', 'utf8');
var certificate = fs.readFileSync('sslcert/cert.pem', 'utf8');
credentials = {
key: privateKey,
cert: certificate
};
var app = express();
// clear console for clean output
console.clear();
var port = process.env.PORT || 4000;
app.set("views", "./views");
app.use(compression());
app.use(logger("dev"));
app.use(device.capture());
app.use(express.static(path.join(__dirname, "./public")));
app.get("/", function (req, res) {
console.log(req.device.type);
if (req.device.type === 'desktop' || req.device.type === 'bot') {
return res.sendFile(path.join(__dirname, './views/desktop.html'));
}
return res.sendFile(path.join(__dirname, './views/index.html'));
});
// start the server
if (ssl) {
https.createServer(credentials, app).listen(port);
console.log(chalk.red("Server started! At https://localhost:" + port));
}
app.listen(port);
console.log(chalk.yellow("Server started! At http://localhost:" + (port)));