Node API Kit
A simple, handy and intuitive NodeJS API toolkit that lazy programmers will love.
Simply install the API Kit with:
npm i @iannisz/node-api-kit
and save yourself a lot of time!
Example
import { createAPI, readJSONBody, createToken } from '@iannisz/node-api-kit'
const api = createAPI(3000)
api.get('/', (req, res) => {
res.end('Hello, World!')
})
api.post('/json', async (req, res) => {
try {
const body = await readJSONBody(req)
const { message } = body
if (message == null) {
res.statusCode = 400
res.end(`Missing "message" field in body`)
return
}
res.end(`I received your message "${ message }"!`)
}
catch {
res.statusCode = 400
res.end(`Bad body`)
}
}, {
needAuth: true
})
console.log(`Test API token: ${ createToken({ user: 'test' }) }`)