From 99774d51f93969748d497a7d2dee7bfb162f9840 Mon Sep 17 00:00:00 2001 From: tts-jwiebe Date: Thu, 13 Jul 2023 11:10:19 +0200 Subject: [PATCH] feat: added logging --- .github/workflows/push.yml | 12 +++++++++--- README.md | 4 +++- server.js | 21 +++++++++++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index a5bb122..81e9cb9 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -27,7 +27,9 @@ jobs: "md": "text/markdown" } - - run: node .github/test-default.js + run: | + node .github/test-default.js + cat log.txt test-index-files: strategy: @@ -50,7 +52,9 @@ jobs: index-files: | ["PULL_REQUEST_TEMPLATE.md"] - - run: node .github/test-index-files.js + run: | + node .github/test-index-files.js + cat log.txt test-allowed-methods: strategy: @@ -73,7 +77,9 @@ jobs: allowed-methods: | ["POST"] - - run: node .github/test-allowed-methods.js + run: | + node .github/test-allowed-methods.js + cat log.txt # draft your next release notes as pull requests are merged into "master" # the configuration is at /.github/release-drafter.yml. diff --git a/README.md b/README.md index 3a47327..4adc1d6 100644 --- a/README.md +++ b/README.md @@ -71,5 +71,7 @@ steps: "xml": "text/xml" } - - run: curl -vvvv http://localhost:8080/index.html + run: | + curl -vvvv http://localhost:8080/index.html + cat log.txt ``` diff --git a/server.js b/server.js index 5302f1b..94e398b 100644 --- a/server.js +++ b/server.js @@ -35,7 +35,24 @@ function deploy(config, ready) { return path.posix.join(...url.split(path.sep)); } + let logger = fs.createWriteStream('log.txt', { + flags: 'a' + }); + let writeLine = (line) => logger.write(`\n${line}`); + server.on('request', (request, response) => { + let now = new Date().toLocaleTimeString(); + let data = ''; + + request.on('data', (chunk) => { + data += chunk; + }); + + request.on('end', () => { + writeLine(`[${now}] ${request.method} ${request.url} ${JSON.stringify(data)}`); + }) + + if (config.noCache) { response.setHeader( 'Cache-Control', @@ -84,7 +101,7 @@ function deploy(config, ready) { } const noIndexFound = config.indexFiles.every(elem => { const indexFile = requestedFile + elem; - if(fs.existsSync(indexFile)){ + if (fs.existsSync(indexFile)) { requestedFile = indexFile; stat = fs.statSync(requestedFile); return false; @@ -92,7 +109,7 @@ function deploy(config, ready) { return true; }); - if(noIndexFound) { + if (noIndexFound) { response.writeHead(200, { 'Content-Type': 'text/html' });