-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (51 loc) · 1.73 KB
/
app.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
var express = require('express');
var colors = require('colors');
var mongoose = require('mongoose');
var app = express();
var port = 5000;
/* api routers*/
var dudeRouter = express.Router();
mongoose.connect('mongodb://localhost/test');
/*Dude's schema*/
var Dude = mongoose.model('Person', {
name: String,
firstName: String,
lastName: String,
birthYear: Number,
siblings: Boolean,
child: Boolean,
description: String,
hobbies: Array
});
/* Generating a default Dude */
var Dude = new Dude({
name: 'Pedro - - Gonçalves',
firstName: 'Pedro',
lastName: 'Gonçalves',
birthYear: 1900,
siblings: true,
child: false,
description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
hobbies: ['surfing', 'live music', 'wannabe guitar player', 'dog walking']
});
/* Saves the default Dude*/
Dude.save(function (err) {
if (err) {
console.log(err);
} else {
console.log(colors.green.underline('smile and wave boys..'));
}
});
/* API router functions - api response options*/
app.get('/dudes', function(req, res){
mongoose.model('Person').find(function(err, dudes){
res.send(dudes);
});
});
/* Example of a api response wich accepts a parameter*/
app.get('/dude:id', function(req, res){
res.send(req.params.id);
});
app.listen(5000, function(err){
console.log(colors.inverse('running server on port' + port));
});