-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor api implementation in next.js style
- Loading branch information
Showing
4 changed files
with
32 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Cors from 'micro-cors' | ||
import request from 'request' | ||
|
||
const cors = Cors({ | ||
allowedMethods: ['GET', 'HEAD'] | ||
}) | ||
|
||
const getCommentCount = (req, res) => { | ||
const { | ||
query: { url } | ||
} = req | ||
|
||
request(`${url}.json`, (error, response, body) => { | ||
if (error || response.statusCode !== 200) { | ||
res.status(404).json({ error }) | ||
} else { | ||
// post_count return all posts including topic itself | ||
const count = JSON.parse(body).posts_count - 1 | ||
|
||
res.status(200).json({ count }) | ||
} | ||
}) | ||
} | ||
|
||
export default cors(getCommentCount) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters