-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
50 lines (41 loc) · 1.25 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
const express = require('express');
const cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
const cors = require('cors');
const env = require('dotenv');
const corsOptions = require('./config/corsOptions');
const userRoutes = require('./routes/userRoutes');
const orderRoutes = require('./routes/orderRoutes');
const restaurantRoutes = require('./routes/restaurantRoutes');
const app = express();
app.use(express.json());
app.use(cookieParser());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
env.config(corsOptions);
const DB_URI = process.env.DB_URI;
const connectDB = async () => {
try {
const conn = await mongoose.connect(DB_URI);
console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.log(error);
process.exit(1);
}
};
const port = process.env.PORT || 2727;
connectDB().then(() => {
app.listen(port, () => {
console.log('listening for requests at 2727');
});
});
app.use('/api/v1/user', userRoutes);
app.use('/api/v1/order', orderRoutes);
app.use('/api/v1/restaurant', restaurantRoutes);
app.all('*', (req, res) => {
res.status(400).json({
status: 'error',
message: `Can't find ${req.originalUrl} on this server!`,
});
});
module.exports = app;