-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-server-unprotected.js
48 lines (36 loc) · 1.09 KB
/
hello-server-unprotected.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict'
const koa = require('koa')
const api = new koa()
const logger = require('koa-logger')
api.use(logger())
const compress = require('koa-compress')
api.use(compress())
const Router = require('koa-router')
const router = new Router()
const dotenv = require('dotenv').config()
if (dotenv.error) {
console.debug('FAILED TO PARSE `.env` FILE | ' + dotenv.error)
}
// handle errors
api.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status === 401 ? 401 : 500;
ctx.body = {}
ctx.app.emit('error', err, ctx)
}
})
api.use(router.routes())
api.use(router.allowedMethods())
router.get('/', async ctx => {
ctx.body = {message: "Hello, World!"}
})
if (!module.parent) {
// To run in a docker container add to the .env file `SERVER_HOSTNAME=0.0.0.0`.
const hostname = dotenv.parsed.SERVER_HOSTNAME || 'localhost'
// The port for the Quickstart Postman collection and cURL examples is 8002
const port = dotenv.parsed.HTTP_PORT || 8002
api.listen(port, hostname)
console.log('Server is now listening at: ' + hostname + ':' + port)
}