server: Log incoming requests on finish
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Jonatan Nilsson 2022-03-29 10:03:35 +00:00
parent fbf2f5958c
commit 4560d20b04
2 changed files with 28 additions and 1 deletions

View file

@ -7,6 +7,33 @@ export function run(http, orgPort, ctx) {
let packageInfo = JSON.parse(readFileSync(packagePath))
const server = http.createServer(function (req, res) {
let finishedRequest = false
var done = function () {
if (finishedRequest) return
finishedRequest = true
let level = 'debug'
if (res.statusCode >= 400) {
level = 'warn'
}
if (res.statusCode >= 500) {
level = 'error'
}
let status = ''
if (res.statusCode >= 400) {
status = res.statusCode + ' '
}
ctx.log[level]({
duration: requestTime,
status: res.statusCode,
}, `<-- ${status}${req.method} ${req.url}`)
}
res.addListener('finish', done);
res.addListener('close', done);
res.writeHead(200);
res.write(JSON.stringify(packageInfo, null, ' '))
res.end()

View file

@ -1,6 +1,6 @@
{
"name": "sc-helloworld",
"version": "2.0.2",
"version": "2.0.3",
"description": "Hello World app for service core",
"main": "index.js",
"scripts": {