-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
41 lines (32 loc) · 1.1 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
/** source/server.ts */
import http from 'http';
import express, { Express } from 'express';
import morgan from 'morgan';
import QRCode from './public/routes/QRCode';
const router: Express = express();
router.use(morgan('dev'));
router.use(express.urlencoded({ extended: false }));
router.use(express.json());
/** RULES OF API */
router.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'origin, X-Requested-With,Content-Type,Accept, Authorization');
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'GET PATCH DELETE POST');
return res.status(200).json({});
}
next();
});
/** Routes */
router.use('/QrCode', QRCode);
/** Error handling */
router.use((req, res, next) => {
const error = new Error('not found');
return res.status(404).json({
message: error.message
});
});
/** Server */
const httpServer = http.createServer(router);
const PORT: any = process.env.PORT ?? 5000;
httpServer.listen(PORT, () => console.log(`The API Server is running on port ${PORT}`));