-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (64 loc) · 2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import express from 'express';
import morgan from 'morgan';
import cors from 'cors';
import NotFound from 'http-errors';
import dotenv from 'dotenv';
import connect_db from './src/config/dbConfig.js';
// IMPORT ROUTERS
import AuthRoute from './src/routes/authRoutes.js';
import UserRoute from './src/routes/userRoutes.js';
import CategoryRoute from './src/routes/categoryRoutes.js';
import RestaurantRoute from './src/routes/restaurantRoutes.js';
import FoodRoute from './src/routes/foodRoutes.js';
import RatingRoute from './src/routes/ratingRoutes.js';
import AddressRoute from './src/routes/addressRoutes.js';
import CartRoute from './src/routes/cartRoutes.js';
import OrderRoute from './src/routes/orderRoutes.js';
dotenv.config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
app.use(cors({ origin: true, credentials: true }));
app.disable('x-powered-by'); //less hacker know about our stack
app.get('/', async (req, res, next) => {
res.send('Awesome it works 🐻');
});
// ROUTES MIDDLEWARES
app.use('/', AuthRoute);
app.use('/api/users', UserRoute);
app.use('/api/categories', CategoryRoute);
app.use('/api/restaurant', RestaurantRoute);
app.use('/api/foods', FoodRoute);
app.use('/api/rating', RatingRoute);
app.use('/api/address', AddressRoute);
app.use('/api/cart', CartRoute);
app.use('/api/order', OrderRoute);
app.use((req, res, next) => {
next(NotFound(404, 'Hush!!! Sorry, Route Not Found'));
});
app.use((err, req, res, next) => {
const errorStatus = err.status || 500;
const errorMessage = err.message || 'Something went wrong';
return res.status(errorStatus).json({
success: false,
status: errorStatus,
message: errorMessage,
stack: err.stack,
});
});
const PORT = process.env.PORT || 7020;
// Initialize Database & Serve Connection
connect_db()
.then(() => {
try {
app.listen(PORT, () => {
console.log(`Server connected 🚀 to http://localhost:${PORT}`);
});
} catch (error) {
console.log('Cannot connect to the server');
}
})
.catch((error) => {
console.log('Invalid database connection...!');
});