-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
68 lines (63 loc) · 2.08 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
import express from 'express'
import * as bodyParser from 'body-parser'
import {MasterRouter} from "./lib/routes/MasterRouter";
import multer from 'multer';
import moment from "moment";
import sequelize from './lib/util/database';
import {User} from "./lib/models/user.model";
import session from "express-session";
import store from 'connect-session-sequelize';
import flash from 'connect-flash';
const SequelizeStore=store(session.Store);
const sessionStore= new SequelizeStore({
db:sequelize
});
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'files');
},
filename: (req, file, cb) => {
cb(null, file.fieldname + '-' + file.originalname);
}
});
const filter = (req,file,cb)=>{
if(file.mimetype==='image/png'||file.mimetype==='image/jpg'||file.mimetype==='image/jpeg')
cb(null,true);
else
cb(null,false);
};
class App {
public app: express.Application;
public mainRouter: MasterRouter = new MasterRouter();
constructor() {
this.app = express();
this.app.use(express.static('public'));
this.app.use('/files',express.static('files'));
this.app.use(bodyParser.json());
this.app.use(multer({storage:fileStorage,fileFilter:filter}).single('image'));
this.app.use(bodyParser.urlencoded({extended: true}));
this.app.use(session({
secret: "my secret",
store: sessionStore,
resave: false,
saveUninitialized: false
}));
this.app.use(flash());
this.app.use((req:any,res:any,next:any)=>{
if(!req.session.user){
return next();
}
User.findByPk(req.session.user.id)
.then(user => {
req.user = user;
next();
})
.catch(err => console.log(err));
});
this.app.set("views", "lib/views");
this.app.set("view engine", "ejs");
this.app.locals.moment=moment;
this.mainRouter.routes(this.app);
}
}
export default new App().app