-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.mjs
59 lines (50 loc) · 1.44 KB
/
server.mjs
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
import express from 'express';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import session from 'express-session';
import router from './routes/index.mjs';
import passport from 'passport';
import Account from './mongoose/schemas/account.mjs';
import './strategies/local-strategy.mjs'
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(
session({
secret: "sessionsecret",
saveUninitialized: false,
resave: false,
cookie : {
maxAge: 60000,
}
})
);
app.use(passport.initialize());
app.use(passport.session())
passport.serializeUser(function(user, done) {
process.nextTick(function() {
done(null, { _id: user._id, username: user.username });
});
});
passport.deserializeUser(function(id, done) {
process.nextTick(async function() {
try { let user = await Account.findById(id).exec();
if(!user) {
throw new Error("User not found");
}
done(null,user);
} catch (err) {
done(err,null);
}
});
});
const HOSTNAME = '127.0.0.1';
const PORT = process.env.port || 3000;
app.set('view engine', 'pug');
app.set('views', './views');
app.use(express.static('public'));
app.use('/', router);
app.listen(PORT, () => {
console.log(`Server running at http://${HOSTNAME}:${PORT}/`);
})