generated from yandeu/phaser-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
73 lines (54 loc) · 2.26 KB
/
app.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
import express from "express";
import bodyParserErrorHandler from "express-body-parser-error-handler";
import listEndpoints from "express-list-endpoints";
import geckos from "@geckos.io/server";
import { createServer } from "http";
import routes from "~/server/routes";
import { isAuthenticated, logger, correctContentType } from "~/server/middleware";
import { serverConfig, GameServer } from "~/server/game";
export const app = express();
const server = createServer(app);
const geckosServer = geckos({ cors: { allowAuthorization: true, origin: "*" } });
geckosServer.addServer(server);
const game = new GameServer(geckosServer, serverConfig);
const rootRoutes = express.Router();
rootRoutes.use(routes);
/* Handle json body in 'Content-Type: application/json' requests */
app.use(express.json());
/* Handle json body parsing errors */
app.use(bodyParserErrorHandler());
app.use(logger("server", ["/"]));
app.use(correctContentType);
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader("Access-Control-Allow-Origin", "*");
// Request methods you wish to allow
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
// Request headers you wish to allow
// res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,Content-Type");
res.setHeader("Access-Control-Allow-Headers", "*");
// Pass to next layer of middleware
next();
});
app.use("/", (req, res) => {
res.status(200).json({ type: "spaceorbit-server", name: "Europe 1" });
});
app.use("/", rootRoutes);
app.use("/endpoints", (req, res) => {
const endpoints = listEndpoints(app);
const endpointsStr = endpoints
.map((endpoint) =>
endpoint.methods.map((method) => `${method} ${endpoint.path}`).join("\n")
)
.join("\n\n");
res.status(200).send(endpointsStr);
});
// Workaround to allow websockets on development using vite-plugin-node
// https://github.com/axe-me/vite-plugin-node/issues/22#issuecomment-1547114428
export const [serverListener] = server.listeners("request");
if (import.meta.env.PROD) {
const port = parseInt(process.env.PORT ?? "") || 3010;
server.listen(port);
console.log("\n http://localhost:" + port + "\n");
}