feat: added sample get endpoint

This commit is contained in:
Roberto Tonino 2021-04-03 19:24:25 +02:00
parent 85cf0e6d13
commit b9b873a915
5 changed files with 37 additions and 2 deletions

View File

@ -1,14 +1,15 @@
import http from 'http'
import express, { Application } from 'express'
import initDebug from 'debug'
import { registerMiddlewares } from './middlewares'
import indexRouter from './routes'
import usersRouter from './routes/users'
import { normalizePort } from './helpers/port'
import { getErrorCb, getListeningCb } from './helpers/server-callbacks'
import { registerMiddlewares } from './middlewares'
import { registerApis } from './routes/api/register'
const PORT = normalizePort(process.env.PORT || '6595')
@ -23,6 +24,9 @@ registerMiddlewares(app)
app.use('/', indexRouter)
app.use('/users', usersRouter)
/* === APIs === */
registerApis(app)
/* === Config === */
app.set('port', PORT)

View File

@ -0,0 +1,3 @@
import sample from './sample'
export default [sample]

View File

@ -0,0 +1,11 @@
import { ApiHandler } from '../../../types'
const path: ApiHandler['path'] = '/sample'
const handler: ApiHandler['handler'] = (_, res) => {
res.send('Mandi')
}
const apiHandler: ApiHandler = { path, handler }
export default apiHandler

View File

@ -0,0 +1,10 @@
import { Application } from 'express'
import getEndpoints from './get'
const prependApiPath = (path:string) => `/api${path}`
export function registerApis (app: Application) {
getEndpoints.forEach((getApi) => {
app.get(prependApiPath(getApi.path), getApi.handler)
})
}

View File

@ -1 +1,8 @@
import { RequestHandler } from 'express'
export type Port = number | string | boolean
export interface ApiHandler {
path: string
handler: RequestHandler
}