-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (28 loc) · 1.11 KB
/
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
25
26
27
28
29
30
31
require('dotenv').config()
const { router, get, post, del } = require('microrouter')
const compose = require('micro-compose')
const { handleErrors } = require('micro-errors')
const cors = require('micro-cors-multiple-allow-origin')
const UrlPattern = require('url-pattern')
const createAuthenticateMiddleware = require('./middlewares/createAuthenticate')
const { connect } = require('./db')
const create = require('./routes/create')
const getList = require('./routes/getList')
const getById = require('./routes/getById')
const deleteById = require('./routes/deleteById')
connect(`mongodb://${process.env.DATABASE_HOSTNAME}:${process.env.DATABASE_PORT}/${process.env.DATABASE_COLLECTION_NAME}`)
module.exports = compose(
handleErrors({ debug: process.env.NODE_ENV !== 'production' }),
cors({
allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
origin: process.env.CORS_ALLOWED_ORIGINS.split(','),
}),
createAuthenticateMiddleware(),
)(
router(
post('/', create),
get('/', getList),
get(new UrlPattern(/^\/([\w-]+)$/, ['id']), getById),
del(new UrlPattern(/^\/([\w-]+)$/, ['id']), deleteById),
)
)