@@ -6,22 +6,22 @@ const { send, createError } = require('micro')
66const db = then ( level ( 'blog-views' ) )
77
88module . exports = async function ( req , res ) {
9- // Check that a blogpost is provided
10- const { pathname } = url . parse ( req . url )
11- if ( pathname . length <= 1 ) {
12- throw createError ( 400 , 'Please include a path to a blogpost.' )
13- }
14- // Get the views
15- let views
16- try {
17- views = parseInt ( await db . get ( pathname ) , 10 )
18- } catch ( err ) {
19- // If the post doesn't have views yet, initialise the post with one view
20- await db . put ( pathname , 1 )
21- send ( res , 200 , { views : 1 } )
22- return
23- }
24- // Increment the views and send them back to client
25- await db . put ( pathname , views + 1 )
26- send ( res , 200 , { views : views + 1 } )
9+ // Check that a blogpost is provided
10+ const { pathname } = url . parse ( req . url )
11+ if ( pathname . length <= 1 ) {
12+ throw createError ( 400 , 'Please include a path to a blogpost.' )
13+ }
14+ // Get the views
15+ let views
16+ try {
17+ views = parseInt ( await db . get ( pathname ) , 10 )
18+ } catch ( err ) {
19+ // If the post doesn't have views yet, initialise the post with one view
20+ await db . put ( pathname , 1 )
21+ send ( res , 200 , { views : 1 } )
22+ return
23+ }
24+ // Increment the views and send them back to client
25+ await db . put ( pathname , views + 1 )
26+ send ( res , 200 , { views : views + 1 } )
2727}
0 commit comments