feishin/server/server.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-10-30 03:12:02 +01:00
/* eslint-disable import/order */
2022-07-26 04:40:16 +02:00
import path from 'path';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import express from 'express';
import passport from 'passport';
import 'express-async-errors';
2022-10-25 06:41:47 +02:00
import { errorHandler } from '@/middleware';
import { routes } from '@routes/index';
2022-10-30 03:12:02 +01:00
import { sockets } from '@sockets/index';
import * as http from 'http';
import * as socketio from 'socket.io';
2022-07-26 04:40:16 +02:00
require('./lib/passport');
const PORT = 9321;
const app = express();
const staticPath = path.join(__dirname, '../feishin-client/');
2022-11-14 05:18:23 +01:00
const filesPath = path.join(__dirname, './files/');
2022-07-26 04:40:16 +02:00
app.use(express.static(staticPath));
2022-11-14 05:18:23 +01:00
app.use('/files', express.static(filesPath));
2022-07-26 04:40:16 +02:00
app.use(
cors({
credentials: false,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
origin: [`http://localhost:4343`, `${process.env.APP_BASE_URL}`],
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
app.get('/', (_req, res) => {
res.sendFile(path.join(staticPath, 'index.html'));
});
app.use(routes);
app.use(errorHandler);
2022-10-30 03:12:02 +01:00
const server = http.createServer(app);
const io = new socketio.Server(server, {
cors: {
credentials: false,
methods: ['GET', 'POST'],
origin: [`http://localhost:4343`, `${process.env.APP_BASE_URL}`],
},
});
app.set('socketio', io);
2022-11-15 22:35:26 +01:00
io.on('connection', (socket) => sockets(socket, io));
2022-10-30 03:12:02 +01:00
server.listen(9321, () => console.log(`Listening on port ${PORT}`));