-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (152 loc) · 5.19 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require('dotenv').config();
const PORT = process.env.PORT || 5000
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const mongo_options = {
useNewUrlParser: true,
useUnifiedTopology: true
};
const postsRoute = require('./routes/posts');
const usersRoute = require('./routes/users');
// Middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use('/post', postsRoute);
app.use('/user', usersRoute);
// Listen to port {PORT}
app.listen(PORT, function () {
console.log("Listening on port " + PORT);
});
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_URI, mongo_options)
.then(() => {
console.log(`Connected to DB!`);
})
.catch(err => {
console.log(`Error occurred! ${err}`);
});
// Routes
app.get('/', (req, res) => {
res.json({
message: "Welcome to the example API home page! Try using the CRUD operations on the endpoints defined",
endpoints: [
{
'/post': [
{
route: '/',
method: 'GET',
definition: 'Returns all posts'
},
{
route: '/:id',
method: 'GET',
definition: 'Returns a specific post'
},
{
route: '/',
method: 'POST',
definition: 'Inserts a new post',
body: {
title: {
'type': 'String',
'required': true
},
description: {
'type': 'String',
'required': true
},
date: {
'type': 'Date'
}
}
},
{
route: '/:id',
method: 'PATCH',
definition: 'Updates an existing post',
body: {
title: {
'type': 'String',
'required': true
},
description: {
'type': 'String',
'required': true
}
}
},
{
route: '/:id',
method: 'DELETE',
definition: 'Deletes a post',
}
]
},
{
'/user': [
{
route: '/',
method: 'GET',
definition: 'Returns all users'
},
{
route: '/:id',
method: 'GET',
definition: 'Returns a specific user'
},
{
route: '/',
method: 'POST',
definition: 'Inserts a new user',
body: {
first_name: {
'type': 'String',
'required': true
},
last_name: {
'type': 'String',
'required': true
},
email: {
'type': 'String',
'required': true
},
phone: {
'type': 'String'
}
}
},
{
route: '/:id',
method: 'PATCH',
definition: 'Updates an existing user',
body: {
first_name: {
'type': 'String',
'required': true
},
last_name: {
'type': 'String',
'required': true
},
email: {
'type': 'String',
'required': true
},
phone: {
'type': 'String',
'required': true
}
}
},
{
route: '/:id',
method: 'DELETE',
definition: 'Deletes an user',
}
]
}
]
});
})