-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (31 loc) · 1.1 KB
/
server.js
File metadata and controls
37 lines (31 loc) · 1.1 KB
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
const http = require("http");
const path = require("path");
const express = require("express");
const { Server } = require("socket.io");
const quoteEngine = require("./lib/quoteEngine");
const { registerTransports } = require("./transport");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.json({ limit: "32kb" }));
const publicDir = path.join(__dirname, "public");
app.use(express.static(publicDir));
app.post("/api/quote/inject", (req, res) => {
const { text, author } = req.body || {};
const result = quoteEngine.injectQuote(text, author);
if (!result.ok) {
return res.status(400).json({ error: result.error });
}
res.json(result.payload);
});
const transportNames = registerTransports({ app, io, quoteEngine });
const PORT = Number(process.env.PORT) || 8000;
server.listen(PORT, () => {
console.log(
`Quotes server on http://localhost:${PORT} (${quoteEngine.poolSize} quotes, rotate every ${quoteEngine.ROTATE_MS / 1000}s)`
);
console.log(
"Transports: " +
transportNames.map((n) => `/transport/${n}`).join(", ")
);
});