Created
May 26, 2023 02:07
-
-
Save fredchu/be26a75a77468a60b5ed0df2bce33f8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 引入所需要的套件 | |
const express = require('express'); | |
const winston = require('winston'); | |
const expressWinston = require('express-winston'); | |
// 創建一個新的 Express 應用 | |
const app = express(); | |
const port = 3000; | |
// 創建一個 Winston 日誌紀錄器,設置輸出檔案 | |
const logger = winston.createLogger({ | |
level: 'info', | |
format: winston.format.json(), | |
transports: [ | |
new winston.transports.File({ filename: 'logs/request.log' }) | |
] | |
}); | |
// 利用 express-winston 套件,在每個 HTTP 請求進來前,先將詳細資訊紀錄到檔案中 | |
app.use(expressWinston.logger({ | |
winstonInstance: logger, | |
})); | |
// 簡單的 GET 請求範例 | |
app.get('/', (req, res) => { | |
res.send('Hello World!') | |
}); | |
// 啟動伺服器 | |
app.listen(port, () => { | |
console.log(`App listening at http://localhost:${port}`) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment