-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
45 lines (34 loc) · 1.14 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const chalk = require('chalk')
require('dotenv').config();
const isAuth = require('./middleware/is-Auth');
//Routes imports
const authRoutes = require('./routes/authRoutes');
//App
const app = express();
//Middleware
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(isAuth);
//Routes listeners
app.use('/auth', authRoutes);
//Errors handler
app.use((error, req, res, next) => {
console.log(chalk.red(error));
const status = error.statusCode || 500;
const message = error.message;
const data = error.data;
res.status(status).json({ message: message, data: data });
});
//Mongoose DB connection and server
mongoose.connect(process.env.DB, {useNewUrlParser:true, useUnifiedTopology:true}, (err,res) =>{
if(err) {
return console.log(chalk.red(`Error - ${err}`))
}
console.log(chalk.green('Database connection success'))
app.listen(process.env.PORT || process.env.ALT_PORT, () => {
console.log(chalk.blue(`Running on port: ${config.port}`))
});
});