mirror of
https://github.com/pawelmalak/snippet-box.git
synced 2025-12-21 21:33:10 +01:00
Initial server code. Building scripts
This commit is contained in:
2
src/config/.env
Normal file
2
src/config/.env
Normal file
@@ -0,0 +1,2 @@
|
||||
PORT=5000
|
||||
NODE_ENV=development
|
||||
10
src/environment.d.ts
vendored
Normal file
10
src/environment.d.ts
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
declare global {
|
||||
namespace NodeJS {
|
||||
interface ProcessEnv {
|
||||
PORT: number;
|
||||
NODE_ENV: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {};
|
||||
16
src/server.ts
Normal file
16
src/server.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import dotenv from 'dotenv';
|
||||
import express from 'express';
|
||||
import { Logger } from './utils';
|
||||
|
||||
// Env config
|
||||
dotenv.config({ path: './src/config/.env' });
|
||||
|
||||
const app = express();
|
||||
const logger = new Logger();
|
||||
const PORT = process.env.PORT;
|
||||
|
||||
app.listen(PORT, () => {
|
||||
logger.log(
|
||||
`Server is working on port ${PORT} in ${process.env.NODE_ENV} mode`
|
||||
);
|
||||
});
|
||||
1
src/typescript/types/ErrorLevel.ts
Normal file
1
src/typescript/types/ErrorLevel.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type ErrorLevel = 'INFO' | 'ERROR' | 'WARN';
|
||||
44
src/utils/Logger.ts
Normal file
44
src/utils/Logger.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { ErrorLevel } from '../typescript/types/ErrorLevel';
|
||||
|
||||
export class Logger {
|
||||
public log(message: string, level: ErrorLevel = 'INFO'): void {
|
||||
console.log(`[${this.generateTimestamp()}] [${level}] ${message}`);
|
||||
}
|
||||
|
||||
private generateTimestamp(): string {
|
||||
const d = new Date();
|
||||
|
||||
// Date
|
||||
const year = d.getFullYear();
|
||||
const month = this.parseDate(d.getMonth() + 1);
|
||||
const day = this.parseDate(d.getDate());
|
||||
|
||||
// Time
|
||||
const hour = this.parseDate(d.getHours());
|
||||
const minutes = this.parseDate(d.getMinutes());
|
||||
const seconds = this.parseDate(d.getSeconds());
|
||||
const miliseconds = this.parseDate(d.getMilliseconds(), true);
|
||||
|
||||
// Timezone
|
||||
const tz = -d.getTimezoneOffset() / 60;
|
||||
|
||||
// Construct string
|
||||
const timestamp = `${year}-${month}-${day} ${hour}:${minutes}:${seconds}.${miliseconds} UTC${
|
||||
tz >= 0 ? '+' + tz : tz
|
||||
}`;
|
||||
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
private parseDate(dateFragment: number, ms: boolean = false): string {
|
||||
if (ms) {
|
||||
if (dateFragment >= 10 && dateFragment < 100) {
|
||||
return `0${dateFragment}`;
|
||||
} else if (dateFragment < 10) {
|
||||
return `00${dateFragment}`;
|
||||
}
|
||||
}
|
||||
|
||||
return dateFragment < 10 ? `0${dateFragment}` : dateFragment.toString();
|
||||
}
|
||||
}
|
||||
1
src/utils/index.ts
Normal file
1
src/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './Logger';
|
||||
Reference in New Issue
Block a user