From 7b67e17c8ed8ee5002abaef44134f9835384e3ee Mon Sep 17 00:00:00 2001 From: Type-Style Date: Fri, 12 Jan 2024 17:09:25 +0100 Subject: [PATCH] [Task] #6 basic log --- package.json | 5 +++-- src/app.ts | 14 +++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 7d9bdd9..c485f04 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,11 @@ "main": "index.js", "type": "module", "scripts": { - "build": "npx tsc && cp -R httpdocs/ dist/", + "clean": "rm -rf dist/*", + "build": "rm -rf dist/* && npx tsc && cp -R httpdocs/ dist/", "build:prod": "npx tsc -p ./tsconfig.prod.json && cp -R httpdocs/ dist/", "start": "node dist/app.js", - "dev": "nodemon src/app.ts", + "dev": "rm -rf dist/* && cp -R httpdocs/ dist/ && nodemon src/app.ts", "lint": "eslint . --fix" }, "keywords": [], diff --git a/src/app.ts b/src/app.ts index 9aafa27..a2daf09 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,14 +1,22 @@ import express from 'express'; import { Request, Response } from 'express'; +import fs from 'fs'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); const app = express(); const port = 80; app.get('/', (req: Request, res: Response) => { - res.send('Hello World, via TypeScript and Node.js!'); - + res.send('Hello World, via TypeScript and Node.js!'); }); app.listen(port, () => { - console.log(`Server running at http://localhost:${port}`); + const date = new Date().toLocaleString('de-DE', { hour12: false }); + const logPath = join(__dirname, 'httpdocs', 'log.txt'); + fs.appendFileSync(logPath, `Express: Server: ${date} \n`); + console.log(`Server läuft unter http://localhost:${port}`); }); \ No newline at end of file