-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
82 lines (80 loc) · 2.06 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
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
const express = require('express');
const session = require('express-session')
const exphbs = require('express-handlebars');
const routers = require("./routers/index");
const cookieParser = require('cookie-parser');
const MongoStore = require("connect-mongo")(session);
const bodyParser = require('body-parser')
const port = process.env.PORT || 3000;
const database = require("./database/index");
let install = false;
const app = express();
database.db.users.then(users => {
users.findOne({
admin: true
}).then(user => {
if (!user) install = true;
})
})
app
.use(session({
resave: false,
saveUninitialized: false,
secret: 'pijamalı hasta yağız şoföre çabucak güvendi',
store: new MongoStore({
url: process.env.BloggerDB || "mongodb://127.0.0.1:27017/BloggerDB"
}),
cookie: {
maxAge: 180 * 60 * 1000
}
}))
.use(bodyParser.json())
.use(express.static("./public"))
.use(cookieParser())
.engine('.hbs', exphbs({
extname: '.hbs'
}))
.set('view engine', '.hbs')
.post("/install", (req, res, next) => {
if (install) {
if (!req.body) return res.json({
error: "Missing fields !",
code: 0
})
if (!req.body.email || !req.body.password) return res.json({
error: "Missing fields !",
code: 0
})
const users = new database.users(req.body.email, req.body.password);
if (users.error) return res.json(users)
users.register(true).then(result => {
if (result.error) return res.json(result);
install = false;
req.session.user = result;
res.json({
result: "ok",
code: 1
})
})
} else {
next();
}
})
.use((req, res, next) => {
if (install) {
res.render("register")
} else {
next();
}
})
.use("/", routers.app)
.use("/api", routers.api)
.use((req, res, next) => {
res.json({
error: "Page not found!",
code: 404
}) // [TODO] 404 page design
})
.listen(port, _ => {
console.log(`Web sitemize ${port} portunda calismaktadir.`);
});