-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
117 lines (92 loc) · 2.87 KB
/
server.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import express = require("express");
import { Server } from "socket.io";
// import path, { dirname } from "path";
import path = require("path");
// import { fileURLToPath } from "url";
// const __dirname = dirname(fileURLToPath(import.meta.url));
import sqlite = require("sqlite3");
/* DB */
sqlite.verbose()
const db = new sqlite.Database("./db/chinook.db", sqlite.OPEN_READWRITE, (err) => {
if (err) return console.error(err);
});
const app = express();
const io = new Server();
/* SERVER RUN */
const server = app.listen(3000, () => {
console.log("Started server at ", server.address());
});
io.attach(server);
import livereload = require("livereload");
import connectLiveReload = require("connect-livereload");
// console.log(process.env.NODE_ENV);
if (process.env.NODE_ENV === 'development') {
// console.log("development mode hehe");
const liveReloadServer = livereload.createServer();
liveReloadServer.server.once("connection", () => {
setTimeout(() => {
liveReloadServer.refresh("/");
}, 50);
});
app.use(connectLiveReload());
}
const staticPath = 'dist' ;
/* Client routes */
// template example, delete later ↓
app.get("/", async (_, res) => {
res.sendFile(process.cwd() + "/dist/pages/index.html");
});
// console.log(process.cwd());
// console.log(__dirname);
// ↑
app.use(express.static(path.join(process.cwd(), staticPath)));
// console.log(path.join(path.join(__dirname, staticPath)));
// console.log(path.join(process.cwd(), staticPath));
app.get('/:page', async (req, res, next) => {
let options = {
root: path.join(process.cwd(), staticPath+'/pages'),
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
}
let fileName = `/${req.params.page}/index.html`;
console.log(fileName);
res.sendFile(fileName, options, (err) => {
// res.sendFile(process.cwd() + "/" + fileName, options, (err) => {
if (err) {
next(err);
} else {
console.log(`Sent: ${fileName}`)
}
})
})
/* APIs */
app.get('/api/test', function (_, res) {
let sql = "SELECT * FROM invoices";
try {
db.all(sql, [], (err, rows) => {
if (err) return res.json({ status: 400, success: false, error: err });
if (rows.length < 1) return res.json({ status: 400, success: false, error: "No match" });
return res.json({ status: 200, data: rows, success: true});
});
} catch (error) {
return res.json({
status: 400,
success: false,
})
}
return
})
/* SOCKET IO */
io.on("connection", (socket) => {
console.log('A user connected: ' + socket.id);
socket.on("chat message", (msg) => {
io.emit("chat message", msg);
});
socket.on('message', (message) => {
console.log(message);
io.emit('message', `${socket.id.slice(0,2)} said ${message}`);
})
});