Skip to content

Commit f60b044

Browse files
committed
Create note endpoint
1 parent 40a96bf commit f60b044

4 files changed

Lines changed: 75 additions & 6 deletions

File tree

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const Router = require('koa-better-router');
22
const Koa = require('koa');
33
const json = require('koa-json');
4+
const bodyParser = require('koa-bodyparser');
5+
46
require('dotenv').config();
57

68
const createRoutes = require('./src/routes');
@@ -19,6 +21,7 @@ api.extend(router);
1921
const app = new Koa();
2022

2123
app.use(json());
24+
app.use(bodyParser());
2225
app.use(router.middleware());
2326
app.use(api.middleware());
2427

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dotenv": "^4.0.0",
88
"koa": "^2.4.1",
99
"koa-better-router": "^2.1.1",
10+
"koa-bodyparser": "^4.2.0",
1011
"koa-json": "^2.0.2",
1112
"mongoose": "^4.13.1"
1213
},

src/routes/notes.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
const createNoteRoutes = ({ router, db }) => {
2-
router.get('/notes', (ctx, next) => {
3-
const Note = db.model('Note');
4-
return Note.find({}, (err, notes) => {
2+
const Note = db.model('Note');
3+
4+
router.get('/notes', (ctx, next) =>
5+
Note.find({}, (err, notes) => {
56
if (err) {
67
return ctx.throw(500, err);
78
}
89
ctx.body = { notes };
910
return next();
11+
}));
12+
13+
router.post('/notes', (ctx, next) => {
14+
const {
15+
title,
16+
description,
17+
} = ctx.request.body;
18+
19+
const note = new Note({
20+
title,
21+
description,
22+
});
23+
24+
return note.save((err) => {
25+
if (err) {
26+
return ctx.throw(500, err);
27+
}
28+
ctx.body = { note };
29+
return next();
1030
});
1131
});
1232
};

yarn.lock

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ builtin-modules@^1.0.0, builtin-modules@^1.1.1:
123123
version "1.1.1"
124124
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"
125125

126+
bytes@3.0.0:
127+
version "3.0.0"
128+
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
129+
126130
caller-path@^0.1.0:
127131
version "0.1.0"
128132
resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-0.1.0.tgz#94085ef63581ecd3daa92444a8fe94e82577751f"
@@ -165,6 +169,15 @@ cli-width@^2.0.0:
165169
version "2.2.0"
166170
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
167171

172+
co-body@^5.1.0:
173+
version "5.1.1"
174+
resolved "https://registry.yarnpkg.com/co-body/-/co-body-5.1.1.tgz#d97781d1e3344ba4a820fd1806bddf8341505236"
175+
dependencies:
176+
inflation "^2.0.0"
177+
qs "^6.4.0"
178+
raw-body "^2.2.0"
179+
type-is "^1.6.14"
180+
168181
co@^4.6.0:
169182
version "4.6.0"
170183
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
@@ -210,6 +223,10 @@ cookies@~0.7.0:
210223
depd "~1.1.1"
211224
keygrip "~1.0.2"
212225

226+
copy-to@^2.0.1:
227+
version "2.0.1"
228+
resolved "https://registry.yarnpkg.com/copy-to/-/copy-to-2.0.1.tgz#2680fbb8068a48d08656b6098092bdafc906f4a5"
229+
213230
core-util-is@~1.0.0:
214231
version "1.0.2"
215232
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
@@ -568,7 +585,7 @@ http-assert@^1.1.0:
568585
deep-equal "~1.0.1"
569586
http-errors "~1.6.1"
570587

571-
http-errors@^1.2.8, http-errors@~1.6.1:
588+
http-errors@1.6.2, http-errors@^1.2.8, http-errors@~1.6.1:
572589
version "1.6.2"
573590
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.6.2.tgz#0a002cc85707192a7e7946ceedc11155f60ec736"
574591
dependencies:
@@ -584,7 +601,7 @@ http-errors@~1.4.0:
584601
inherits "2.0.1"
585602
statuses ">= 1.2.1 < 2"
586603

587-
iconv-lite@^0.4.17:
604+
iconv-lite@0.4.19, iconv-lite@^0.4.17:
588605
version "0.4.19"
589606
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"
590607

@@ -596,6 +613,10 @@ imurmurhash@^0.1.4:
596613
version "0.1.4"
597614
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
598615

616+
inflation@^2.0.0:
617+
version "2.0.0"
618+
resolved "https://registry.yarnpkg.com/inflation/-/inflation-2.0.0.tgz#8b417e47c28f925a45133d914ca1fd389107f30f"
619+
599620
inflight@^1.0.4:
600621
version "1.0.6"
601622
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
@@ -757,6 +778,13 @@ koa-better-router@^2.1.1:
757778
methods "^1.1.2"
758779
path-match "^1.2.4"
759780

781+
koa-bodyparser@^4.2.0:
782+
version "4.2.0"
783+
resolved "https://registry.yarnpkg.com/koa-bodyparser/-/koa-bodyparser-4.2.0.tgz#bce6e08bc65f8709b6d1faa9411c7f0d8938aa54"
784+
dependencies:
785+
co-body "^5.1.0"
786+
copy-to "^2.0.1"
787+
760788
koa-compose@^3.0.0, koa-compose@^3.1.0:
761789
version "3.2.1"
762790
resolved "https://registry.yarnpkg.com/koa-compose/-/koa-compose-3.2.1.tgz#a85ccb40b7d986d8e5a345b3a1ace8eabcf54de7"
@@ -1121,6 +1149,19 @@ pseudomap@^1.0.2:
11211149
version "1.0.2"
11221150
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
11231151

1152+
qs@^6.4.0:
1153+
version "6.5.1"
1154+
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.1.tgz#349cdf6eef89ec45c12d7d5eb3fc0c870343a6d8"
1155+
1156+
raw-body@^2.2.0:
1157+
version "2.3.2"
1158+
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.3.2.tgz#bcd60c77d3eb93cde0050295c3f379389bc88f89"
1159+
dependencies:
1160+
bytes "3.0.0"
1161+
http-errors "1.6.2"
1162+
iconv-lite "0.4.19"
1163+
unpipe "1.0.0"
1164+
11241165
read-pkg-up@^2.0.0:
11251166
version "2.0.0"
11261167
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be"
@@ -1380,7 +1421,7 @@ type-check@~0.3.2:
13801421
dependencies:
13811422
prelude-ls "~1.1.2"
13821423

1383-
type-is@^1.5.5:
1424+
type-is@^1.5.5, type-is@^1.6.14:
13841425
version "1.6.15"
13851426
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.15.tgz#cab10fb4909e441c82842eafe1ad646c81804410"
13861427
dependencies:
@@ -1391,6 +1432,10 @@ typedarray@^0.0.6:
13911432
version "0.0.6"
13921433
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
13931434

1435+
unpipe@1.0.0:
1436+
version "1.0.0"
1437+
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"
1438+
13941439
util-deprecate@~1.0.1:
13951440
version "1.0.2"
13961441
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"

0 commit comments

Comments
 (0)