├── .gitignore
├── .idea
├── misc.xml
├── vcs.xml
├── modules.xml
├── resthub2.iml
└── workspace.xml
├── package.json
├── contactModel.js
├── api-routes.js
├── README.md
├── index.js
└── contactController.js
/.gitignore:
--------------------------------------------------------------------------------
1 | ./node_modules
2 | node_modules
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/resthub2.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "resthub2",
3 | "version": "2.0.0",
4 | "description": "A Node App demonstrating simple RESTFul API implementation",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1",
8 | "start": "node index.js"
9 |
10 | },
11 | "keywords": [
12 | "API",
13 | "resful",
14 | "json",
15 | "node",
16 | "mongodb",
17 | "express"
18 | ],
19 | "author": "David Inyang-Etoh",
20 | "license": "ISC",
21 | "dependencies": {
22 | "body-parser": "^1.20.1",
23 | "express": "^4.18.2",
24 | "mongoose": "^5.13.16"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/contactModel.js:
--------------------------------------------------------------------------------
1 | // contactModel.js
2 | var mongoose = require('mongoose');
3 | // Setup schema
4 | var contactSchema = mongoose.Schema({
5 | name: {
6 | type: String,
7 | required: true
8 | },
9 | email: {
10 | type: String,
11 | required: true
12 | },
13 | gender: String,
14 | phone: String,
15 | create_date: {
16 | type: Date,
17 | default: Date.now
18 | }
19 | });
20 | // Export Contact model
21 | var Contact = module.exports = mongoose.model('contact', contactSchema);
22 | module.exports.get = function (callback, limit) {
23 | Contact.find(callback).limit(limit);
24 | }
--------------------------------------------------------------------------------
/api-routes.js:
--------------------------------------------------------------------------------
1 | // api-routes.js
2 | // Initialize express router
3 | let router = require('express').Router();
4 | // Set default API response
5 | router.get('/', function (req, res) {
6 | res.json({
7 | status: 'API Its Working',
8 | message: 'Welcome to RESTHub crafted with love!',
9 | });
10 | });
11 | // Import contact controller
12 | var contactController = require('./contactController');
13 | // Contact routes
14 | router.route('/contacts')
15 | .get(contactController.index)
16 | .post(contactController.new);
17 |
18 | router.route('/contacts/:contact_id')
19 | .get(contactController.view)
20 | .patch(contactController.update)
21 | .put(contactController.update)
22 | .delete(contactController.delete);
23 |
24 |
25 | // Export API routes
26 | module.exports = router;
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # REST HUb 2.0
2 |
3 | An app demonstrating simple API implementation with NodeJs, Express and MongoDb
4 |
5 | The companion tutorial on building can be found here [How To Build Simple RESTful API With NodeJs, ExpressJs And MongoDb](https://medium.com/@dinyangetoh/how-to-build-simple-restful-api-with-nodejs-expressjs-and-mongodb-99348012925d)
6 |
7 | The `api` uri preceed all API endpoints and the following endpoints are currently available
8 | * GET `/api/contacts`
9 | * POST `/api/contacts`
10 | * GET `/api/contacts/:id`
11 | * PUT `/api/contacts/:id`
12 | * PATCH `/api/contacts/:id`
13 | * DELETE `/api/contacts/:id`
14 |
15 | The live app is available on heroku here
16 | https://resthub2.herokuapp.com
17 |
18 |
19 |
20 | Get in Touch
21 | ===============
22 |
23 | I am available on twitter, facebook, linkedin, gmail @dinyangetoh
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | // Import express
2 | let express = require('express');
3 | // Import Body parser
4 | let bodyParser = require('body-parser');
5 | // Import Mongoose
6 | let mongoose = require('mongoose');
7 | // Initialize the app
8 | let app = express();
9 |
10 | // Import routes
11 | let apiRoutes = require("./api-routes");
12 | // Configure bodyparser to handle post requests
13 | app.use(bodyParser.urlencoded({
14 | extended: true
15 | }));
16 | app.use(bodyParser.json());
17 | // Connect to Mongoose and set connection variable
18 | mongoose.connect('mongodb://localhost/resthub', { useNewUrlParser: true});
19 |
20 | // Heroku Mongoose connection
21 | // mongoose.connect('mongodb://heroku_5686p02g:sia8l3fni4jmu7qbn0ac1t75mf@ds349857.mlab.com:49857/heroku_5686p02g', { useNewUrlParser: true });
22 |
23 | var db = mongoose.connection;
24 |
25 | // Added check for DB connection
26 |
27 | if(!db)
28 | console.log("Error connecting db")
29 | else
30 | console.log("Db connected successfully")
31 |
32 | // Setup server port
33 | var port = process.env.PORT || 8080;
34 |
35 | // Send message for default URL
36 | app.get('/', (req, res) => res.send('Hello World with Express'));
37 |
38 | // Use Api routes in the App
39 | app.use('/api', apiRoutes);
40 | // Launch app to listen to specified port
41 | app.listen(port, function () {
42 | console.log("Running RestHub on port " + port);
43 | });
--------------------------------------------------------------------------------
/contactController.js:
--------------------------------------------------------------------------------
1 | // contactController.js
2 | // Import contact model
3 | Contact = require('./contactModel');
4 | // Handle index actions
5 | exports.index = function (req, res) {
6 | Contact.get(function (err, contacts) {
7 | if (err) {
8 | res.json({
9 | status: "error",
10 | message: err,
11 | });
12 | }
13 | res.json({
14 | status: "success",
15 | message: "Contacts retrieved successfully",
16 | data: contacts
17 | });
18 | });
19 | };
20 | // Handle create contact actions
21 | exports.new = function (req, res) {
22 | var contact = new Contact();
23 | contact.name = req.body.name ? req.body.name : contact.name;
24 | contact.gender = req.body.gender;
25 | contact.email = req.body.email;
26 | contact.phone = req.body.phone;
27 | // save the contact and check for errors
28 | contact.save(function (err) {
29 | // Check for validation error
30 | if (err)
31 | res.json(err);
32 | else
33 | res.json({
34 | message: 'New contact created!',
35 | data: contact
36 | });
37 | });
38 | };
39 | // Handle view contact info
40 | exports.view = function (req, res) {
41 | Contact.findById(req.params.contact_id, function (err, contact) {
42 | if (err)
43 | res.send(err);
44 | res.json({
45 | message: 'Contact details loading..',
46 | data: contact
47 | });
48 | });
49 | };
50 | // Handle update contact info
51 | exports.update = function (req, res) {
52 | Contact.findById(req.params.contact_id, function (err, contact) {
53 | if (err)
54 | res.send(err);
55 | contact.name = req.body.name ? req.body.name : contact.name;
56 | contact.gender = req.body.gender;
57 | contact.email = req.body.email;
58 | contact.phone = req.body.phone;
59 | // save the contact and check for errors
60 | contact.save(function (err) {
61 | if (err)
62 | res.json(err);
63 | res.json({
64 | message: 'Contact Info updated',
65 | data: contact
66 | });
67 | });
68 | });
69 | };
70 | // Handle delete contact
71 | exports.delete = function (req, res) {
72 | Contact.remove({
73 | _id: req.params.contact_id
74 | }, function (err, contact) {
75 | if (err)
76 | res.send(err);
77 | res.json({
78 | status: "success",
79 | message: 'Contact deleted'
80 | });
81 | });
82 | };
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 |
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 |
162 |
163 |
164 |
165 |
166 |
167 |
168 | 1562826786151
169 |
170 |
171 | 1562826786151
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
--------------------------------------------------------------------------------