-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathserver.js
48 lines (39 loc) · 1.71 KB
/
server.js
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
/** using import statements in the electron / node files breaks npm start and nodepty
* - types are left in place in these files for future iteration alternate import method is required for them to function
*/
// import { Application, ErrorRequestHandler, NextFunction, Request, Response, Router } from "express";
// import { Authenticator } from "passport";
// import { defaultErrType } from "./utils/backendTypes";
const express = require('express');
const app/*: Application*/ = express();
const cookieParser/*: Function*/ = require('cookie-parser');
const router/*: Router*/ = require('./routes/router');
const PORT/*: number */= 3001;
const passport/*: Authenticator*/ = require('passport');
// dotenv.config({ path: './config/config.env' });
require('./config/passport')(passport);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Initialize passport session
app.use(passport.initialize());
// app.use(passport.session());
app.use('/', router);
// Any other request is caught here
app.use((req/*: Request*/, res/*: Response*/)/*: Response*/ => res.status(404).send('Error 404: No content found'));
// Express global error handler
app.use((err/*: ErrorRequestHandler*/, req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: Response*/ => {
const defaultErr/*: defaultErrType*/ = {
log: 'Express error handler caught unknown middleware error',
status: 500,
message: 'An error occurred',
};
const errorObj = Object.assign({}, defaultErr, err);
return res.status(errorObj.status).json(err);
});
// Start server
app.listen(PORT, ()/*: void*/ => {
console.log(`TEST Server listening on port: ${PORT}`);
});
module.exports = app;
module.exports = app;