This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (86 loc) · 2.5 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require('dotenv').config()
const express = require('express');
const cors = require('cors');
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
const mongoSanitize = require('express-mongo-sanitize');
const { connectDB } = require('./src/database');
const port = process.env.PORT || 3000;
const app = express()
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'IS2 Shop Online',
version: '1.0.0',
},
servers: [{
url: "http://localhost:3000/api/",
description: "Development server"
},
{
url: "https://is2shoponline.herokuapp.com/api/",
description: "Production server"
}],
components: {
securitySchemes: {
token: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
name: "token",
in: "header"
}
}
}
},
apis: [
'./src/routes/users.routes.js',
'./src/routes/products.routes.js',
'./src/routes/cart.routes.js',
'./src/routes/orders.routes.js',
], // files containing annotations as above
};
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
const swaggerSpec = swaggerJSDoc(options);
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.use(express.json()); //Used to parse JSON bodies
app.use(express.urlencoded({ extended: true })); //Parse URL-encoded bodies
app.use(mongoSanitize());
app.use(cors());
// router
const usersRouter = require('./src/routes/users.routes');
const productsRouter = require('./src/routes/products.routes');
const cartRouter = require('./src/routes/cart.routes.js');
const ordersRouter = require('./src/routes/orders.routes');
// files
app.use('/', express.static('public'));
// api
app.use('/api/v2/users', usersRouter);
app.use('/api/v2/products', productsRouter);
app.use('/api/v2/cart', cartRouter);
app.use('/api/v2/orders', ordersRouter.router);
app.use('/api/v1/orders', ordersRouter.router_v1);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
res.status(404).json({
message: "No such route exists"
})
});
// catch 500 and forward to error handler
app.use(function (err, req, res, next) {
res.status(500).json({
message: err
})
});
if (process.env.NODE_ENV !== 'test') {
try {
connectDB()
} catch (error) {
console.log("database error")
}
}
var server = app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
module.exports = { app, server };