Skip to content

Commit f433ae4

Browse files
committed
Add differentiation between GET and POST requests
1 parent b011497 commit f433ae4

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@ module.exports = async function (req, res) {
1111
if (pathname.length <= 1) {
1212
throw createError(400, 'Please include a path to a page.')
1313
}
14-
if (req.method !== 'GET') {
15-
throw createError(400, 'Please make a GET request.')
14+
if (req.method !== 'GET' && req.method !== 'POST') {
15+
throw createError(400, 'Please make a GET or a POST request.')
1616
}
1717
try {
1818
const views = parseInt(await db.get(pathname), 10)
1919
// Increment the views and send them back to client
2020
await db.put(pathname, views + 1)
21-
send(res, 200, { views: views + 1 })
21+
if (req.method === 'GET') {
22+
send(res, 200, { views: views + 1 })
23+
} else {
24+
send(res, 200)
25+
}
2226
} catch (err) {
2327
if (err.notFound) {
2428
// Initialise the page with one view
2529
await db.put(pathname, 1)
26-
send(res, 200, { views: 1 })
30+
if (req.method === 'GET') {
31+
send(res, 200, { views: 1 })
32+
} else {
33+
send(res, 200)
34+
}
2735
} else {
28-
throw createError(500, 'Something went wrong, sorry about that.')
36+
throw createError(500, 'Internal server error.')
2937
}
3038
}
3139
}

0 commit comments

Comments
 (0)