-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (91 loc) · 2.77 KB
/
index.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Module dependencies.
*/
const debug = require('debug')('todolist:server');
const http = require('http');
const createError = require('http-errors');
const express = require('express');
const dotenv = require('dotenv');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
const session = require('express-session');
const app = express();
const server = http.createServer(app);
const socketIO = require('socket.io');
const socketAllowedOrigins = ['http://localhost:3366', 'https://todo-fe-six.vercel.app'];
const io = socketIO(
server,
{
cors: {
origin: socketAllowedOrigins,
}
}
);
global.io = io;
dotenv.config();
// const rmUnusedImages = require('./lib/middleware/rm_unused_images');
const authVerify = require('./lib/middleware/authVerify');
const todoRouter = require('./routes/todo');
const subtaskRouter = require('./routes/subtask');
const userRouter = require('./routes/user');
const emailRouter = require('./routes/email');
const listRouter = require('./routes/list');
app.set('serverPath', process.env.NODE_ENV === 'PROD' ? 'https://todo-be-psi.vercel.app/' : 'http://localhost:8000/');
app.set('imagesPath', path.join(__dirname, '/public/images/'));
app.set('todoAttachmentFilePath', path.join(__dirname, '/public/todo-attachment/'));
app.set('xlsxsPath', path.join(__dirname, '/storage/inputs/xlsxs/'));
app.use(session({
secret: "keyboard cat",
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 365 * 24 * 60 * 60 * 1000,
secure: false,
httpOnly: true,
sameSite: 'strict',
},
rolling: true,
}));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
const allowedOrigins = ['http://localhost:3366', 'https://todo-fe-six.vercel.app'];
app.use(cors({
origin: allowedOrigins,
credentials: true,
}));
// rm unused images in public folder
// app.use(rmUnusedImages);
// auth verrify
const WHITELIST_URLs = [
'/user/register',
'/user/login',
'/user/logout',
];
app.use('*', (req, res, next) => {
if (WHITELIST_URLs.includes(req.originalUrl)) {
next();
} else {
authVerify.validateToken(req, res, next);
}
});
app.use('/todo', todoRouter);
app.use('/subtask', subtaskRouter);
app.use('/user', userRouter);
app.use('/email', emailRouter);
app.use('/list', listRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});
const PORT = process.env.PORT || 8000;
server.listen(PORT, () => console.log(`Server is running in port ${PORT}`));