-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (90 loc) · 2.28 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
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
var express = require('express');
var app = express();
const bodyParser = require('body-parser')
app.use(bodyParser.json())
const connectToDatabase = require('./db');
const Note = require('./models/Note');
app.get('/', function getProject(req, res) {
res.send({
"Project":"Hello this a Rest API for simple Notes",
"URL":"https://github.com/Laudenlaruto/Node-Rest-api-test/"
})
})
app.post('/note', function createNote(req, res){
const note = {
title: req.body.title,
description: req.body.description
}
connectToDatabase()
.then(() => {
Note.create(note)
.then(function(note) {
console.log(note);
res.status(200).json(note);
})
.catch(function(note){
res.status(500).json(note);
});
});
});
app.get('/note/:id', function getOneNote(req, res){
connectToDatabase()
.then(() =>{
Note.findById(req.params.id)
.then((note) =>{
res.send({
"title":note.title,
"description":note.description
});
})
.catch((err) => {
res.send(err.message);
});
});
});
app.get('/notes', function getAllNotes(req, res) {
connectToDatabase()
.then(() =>{
Note.find()
.then((notes) =>{
console.log(notes);
res.send({
"Status":"ok",
"Notes":notes
});
})
.catch((err) =>{
res.send(err.message)
});
})
});
app.patch('/note/:id',function updateNote(req, res) {
const note = {
title: req.body.title,
description: req.body.description
}
connectToDatabase()
.then(() =>{
Note.findByIdAndUpdate(req.params.id, note,{new: true})
.then((note) =>{
res.send({note})
})
.catch((err) =>{
res.send(err.message)
});
});
});
app.delete('/note/:id', function deleteNote(req, res) {
connectToDatabase()
.then(() =>{
Note.findByIdAndRemove(req.params.id)
.then((note) =>{
res.send({message : 'Removed note with id: ' + note._id, note: note})
}).catch((err) =>{
res.send(err.message)
});
})
});
app.listen(3000);
// Export your Express configuration so that it can be consumed by the Lambda handler
module.exports = app