forked from oliverplay/nodejs-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (39 loc) · 1.13 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
const express = require('express');
const logger = require('morgan');
const cors = require('cors');
const path = require('path');
require('dotenv').config();
const contactsRouter = require('./routes/api/contacts');
const usersRouter = require('./routes/api/users');
const app = express();
app.use(express.static('public'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
const formatsLogger =
app.get('env') === 'development' ? 'dev' : 'short';
app.use(logger(formatsLogger));
app.use(cors());
app.use(express.json());
require('./config/config-passport');
app.use('/api/contacts', contactsRouter);
app.use('/api/users', usersRouter);
app.use((_, res, __) => {
res.status(404).json({
status: 'error',
code: 404,
message: 'You need to use /api route!',
data: 'Not found',
});
});
app.use((err, _, res, __) => {
console.log(err.stack);
res.status(500).json({
status: 'fail',
code: 500,
message: err.message,
data: 'Internal Server Error',
});
});
module.exports = app;