-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
64 lines (53 loc) · 1.83 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
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
const colors = require('colors');
// to saving user credentials in th cookie
const cookieParser = require('cookie-parser');
const expressValidator = require('express-validator');
require('dotenv').config();
const authUserRoutes = require('./routes/authUser');
const userRoutes = require('./routes/userRoutes');
const categoryRoutes = require('./routes/categoryRoutes');
const productRoutes = require('./routes/productRoutes');
const braintreeRoutes = require('./routes/braintreeRoutes');
const orderRoutes = require('./routes/orderRoutes');
const app = express();
// Connect to Atlas MongoDB
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.then(() => console.log('Online Marketplace Atlas MongoDB Connected!'));
mongoose.connection.on('err', err => {
console.log(
`Online Marketplace Atlas MongoDB connection error: ${err.message}`.cyan
.underline.bold
);
});
// middlewares
// trace logs like this "GET /api/signup 404 0.602 ms - 149" in terminal
app.use(morgan('dev'));
// get json data from request body
app.use(bodyParser.json());
app.use(cookieParser());
app.use(expressValidator());
/** cors with our API will be able to handle the request are coming form different origin */
app.use(cors());
// express routes middleware
app.use('/api', authUserRoutes);
app.use('/api', userRoutes);
app.use('/api', categoryRoutes);
app.use('/api', productRoutes);
app.use('/api', braintreeRoutes);
app.use('/api', orderRoutes);
const port = process.env.PORT || 7001;
app.listen(port, () => {
console.log(
`Online-Marketplace Server is running on port ${port}`.rainbow.bold
);
});