This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
71 lines (57 loc) · 1.83 KB
/
app.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
require('dotenv').config();
const express = require('express');
const expressAsyncErrors = require('./express-async-errors');
const mongoose = require('mongoose');
const path = require('path');
const {log} = require('./utils');
const mainRoute = require('./routes/main');
const argeRoute = require('./routes/arge');
const courseRoute = require('./routes/course');
const eventRoute = require('./routes/event');
const faqRoute = require('./routes/faq');
const formRoute = require('./routes/form');
const memberRoute = require('./routes/member');
const {handleErrorResponse} = require('./middleware');
const DB_URL = process.env.DB_URL;
const PORT = process.env.NODE_LOCAL_PORT;
const app = express();
// Middleware
app.use(express.urlencoded({extended: false}));
app.use(express.json());
app.use('/public', express.static(path.join(__dirname, 'public')));
// Routes
app.use('/', mainRoute);
app.use('/', formRoute);
app.use('/arge', argeRoute);
app.use('/courses', courseRoute);
app.use('/events', eventRoute);
app.use('/faq', faqRoute);
app.use('/members', memberRoute);
// In case there isn't any route
app.use(function (req, res, next) {
if (!req.route) {
const error = {
status: 404,
title: 'Sayfa Bulunamadı',
message: 'Sanırsam yolunu şaşırdın!'
};
return next(error);
}
next();
});
// Error handler middleware
app.use(handleErrorResponse);
// View Engine
app.set('view engine', 'ejs');
mongoose.connect(DB_URL, {useNewUrlParser: true, useUnifiedTopology: true});
const db = mongoose.connection;
db.once('open', () => {
log(`Connected to database at DB_URL: "${DB_URL}"`);
});
db.on('error', (error) => {
log(`Database connection failed, error: "${error}"`, 'ERROR');
});
app.listen(PORT, () => {
log(`Server is listening on PORT: "${PORT}"`);
});
module.exports = app;