-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
72 lines (60 loc) · 1.8 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
/* -------------------------------------------------------------------------- */
/* Dependencies */
/* -------------------------------------------------------------------------- */
// Packages
const express = require('express');
const dotenv = require('dotenv');
const morgan = require('morgan');
const bodyParser = require('body-parser');
var cors = require('cors');
const path = require('path');
const mongoose = require('mongoose');
// Swagger
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./src/swagger.json');
// config
dotenv.config();
// const
const app = express();
// DATABASE CONNECTION
mongoose.set('strictQuery', false);
mongoose.connect(
process.env.NODE_ENV === 'production'
? process.env.PROD_DATABASE
: process.env.DEV_DATABASE,
(err) => {
if (err) {
console.log(err);
} else {
console.log('Connected to the database');
}
},
);
// Middleware
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'uploads')));
app.use(cors());
// Require APIs
const userRoutes = require('./src/routes/user.route');
const authRoutes = require('./src/routes/auth.route');
// local APIs
app.use('/v1/api', authRoutes);
app.use('/v1/api', userRoutes);
// API for uploads file (photo, galleries)
app.get('/uploads/:id', (req, res) => {
res.sendFile(path.join(__dirname, `./uploads/${req.params.id}`));
});
// API for swagger documentation
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// PORT
const PORT = process.env.PORT || 5000;
app.listen(PORT, (err) => {
if (err) {
console.log(err);
} else {
console.log(`Listening on PORT ${PORT}`);
}
});
module.exports = app;