Skip to content

Commit

Permalink
Refactor api implementation in next.js style
Browse files Browse the repository at this point in the history
  • Loading branch information
iAdramelk committed Dec 4, 2019
1 parent 505077c commit 0d4e8f1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"husky": "^3.1.0",
"jest": "^24.9.0",
"lint-staged": "^10.0.0-1",
"micro-cors": "^0.1.1",
"prettier": "^1.19.1",
"pretty-quick": "^2.0.1",
"request": "^2.88.0"
Expand Down
25 changes: 25 additions & 0 deletions pages/api/comments.js
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)
15 changes: 1 addition & 14 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const querystring = require('querystring')
const request = require('request')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const app = next({ dev, crossOrigin: 'anonymous' })
const port = process.env.PORT || 3000
const handle = app.getRequestHandler()

Expand Down Expand Up @@ -110,18 +109,6 @@ app.prepare().then(() => {
} else {
app.render(req, res, '/doc', query)
}
} else if (/^\/api\/comments/i.test(pathname)) {
request(`${query.url}.json`, (error, response, body) => {
if (error || response.statusCode !== 200) {
res.write(JSON.stringify({ error }))
} else {
// post_count return all posts including topic itself
const count = JSON.parse(body).posts_count - 1

res.write(JSON.stringify({ count }))
}
res.end()
})
} else {
handle(req, res, parsedUrl)
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5813,6 +5813,11 @@ merge-stream@^2.0.0:
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==

micro-cors@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/micro-cors/-/micro-cors-0.1.1.tgz#af7a480182c114ffd1ada84ad9dffc52bb4f4054"
integrity sha512-6WqIahA5sbQR1Gjexp1VuWGFDKbZZleJb/gy1khNGk18a6iN1FdTcr3Q8twaxkV5H94RjxIBjirYbWCehpMBFw==

microevent.ts@~0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/microevent.ts/-/microevent.ts-0.1.1.tgz#70b09b83f43df5172d0205a63025bce0f7357fa0"
Expand Down

0 comments on commit 0d4e8f1

Please sign in to comment.