forked from maccyber/micro-bigfive-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
24 lines (22 loc) · 731 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'use strict'
const { parse } = require('url')
const { json, send } = require('micro')
const readFileSync = require('fs').readFileSync
const marked = require('marked')
const calculateScore = require('./lib/calculate-score')
module.exports = async (req, res) => {
let result = {}
const {query} = await parse(req.url, true)
const data = req.method === 'POST' ? await json(req) : query
if (req.method === 'POST') {
const scores = calculateScore(data.answers)
data.data = scores
result = data
} else {
const readme = readFileSync('./README.md', 'utf-8')
result = marked(readme)
}
let status = result.error ? 500 : 200
res.setHeader('Access-Control-Allow-Origin', '*')
send(res, status, result)
}