-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (51 loc) · 1.82 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
require('dotenv').config();
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const PORT = process.env.PORT || 3500;
// Third-party middleware(s) imports:
const CORS = require('cors');
const cookie_parser = require('cookie-parser');
// Inter-links:
// 1) Configuration:
const CORS_options = require('./configuration/CORS_options');
const mongodb_connection = require('./configuration/Mongodb_connection');
// 2) Middleware:
const Credentials = require('./middleware/Credentials');
const Req_logger = require('./middleware/Req_logger');
const Verify_JWT = require('./middleware/Verify_JWT');
// 3) Routes:
const auth = require('./routes/Auth');
const buyer = require('./routes/api/Buyer');
const seller = require('./routes/api/Seller');
const Unmatched = require('./routes/Unmatched');
// MongoDB connection:
mongodb_connection();
// Custom middleware for logging the req's method and path in console:
app.use(Req_logger);
// Setting 'Acess-Control-Allowed-Origins' header to 'true' for all allowed origins:
app.use(Credentials);
// Third-party CORS middleware:
app.use(CORS(CORS_options));
// Third-party cookie-parsing middleware:
app.use(cookie_parser());
// Express in-built middleware for parsing place_holders from URL:
app.use(express.urlencoded({ extended: false }));
// Express in-built middleware for parsing JSON paylod:
app.use(express.json());
// Express in-built middleware for routing static files:
app.use(express.static(path.join(__dirname, '/public')));
app.use('/auth', auth);
app.use(Verify_JWT);
app.use('/buyer', buyer);
app.use('/seller', seller);
app.all('*', Unmatched);
mongoose.connection.once('open', () =>
{
console.log('Connected to MongoDB');
app.listen(PORT, () =>
{
console.log('The server is running in port: '+PORT);
});
});