-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (43 loc) · 1.09 KB
/
index.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
import express from 'express';
import accountsRouter from './routes/account.routes.js';
import winston from 'winston';
import { promises as fs } from 'fs';
const { readFile, writeFile } = fs;
global.fileName = 'accounts.json';
const { combine, timestamp, label, printf } = winston.format;
const myFormat = printf(({ level, message, label, timestamp}) => {
return `${timestamp} [${label}] ${level}: ${message}`;
})
global.logger = winston.createLogger({
level: "silly",
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({filename: "my-bank-api.log"})
],
format: combine(
label({ label: "my-bank-api"}),
timestamp(),
myFormat
)
})
const app = express();
app.use(express.json());
app.use('/account', accountsRouter);
app.listen(3000, async () => {
try {
await readFile(fileName);
logger.info('API STARTED');
} catch (err) {
const initialJson = {
nextId: 1,
accounts: [],
};
writeFile(fileName, JSON.stringify(initialJson))
.then(() => {
logger.info('API STARTED and File Created');
})
.catch((err) => {
logger.error(err);
});
}
});