@@ -2,27 +2,19 @@ import { Bonjour } from "bonjour-service"; // you must install this
22
33import cors from "cors" ;
44import express from "express" ;
5+ import morgan from "morgan" ;
56import fetch from "node-fetch" ;
7+ import * as rfs from "rotating-file-stream" ;
68import { exec , spawn } from "child_process" ;
79import fs from "fs" ;
810import os from "os" ;
911import path from "path" ;
1012import { fileURLToPath } from "url" ;
11- import util from "util" ;
1213
1314// Get absolute path to this script (because import.meta.url gives a file:// URL)
1415const __filename = fileURLToPath ( import . meta. url ) ;
1516const __dirname = path . dirname ( __filename ) ;
1617
17- var log_file = fs . createWriteStream ( __dirname + "/server.log" , { flags : "w" } ) ;
18- var log_stdout = process . stdout ;
19-
20- console . log = function ( d ) {
21- //
22- log_file . write ( util . format ( d ) + "\n" ) ;
23- log_stdout . write ( util . format ( d ) + "\n" ) ;
24- } ;
25-
2618// Read version.json using fs since require is not available in ES modules
2719let version = "" ;
2820let versionFilePath = path . join ( __dirname , "version.json" ) ;
@@ -39,6 +31,21 @@ if (fs.existsSync(versionFilePath)) {
3931
4032const app = express ( ) ;
4133
34+ app . use (
35+ morgan ( "combined" , {
36+ stream : rfs . createStream (
37+ ( date , index ) =>
38+ date
39+ ? `${ date . getFullYear ( ) } /${ date . getMonth ( ) } -${ date . getDate ( ) } .log`
40+ : "server.log" ,
41+ {
42+ interval : "1d" ,
43+ path : path . join ( __dirname , "log" ) ,
44+ }
45+ ) ,
46+ } )
47+ ) ;
48+
4249const allowedOrigins = [
4350 "http://localhost" ,
4451 "http://localhost:3000" ,
@@ -56,7 +63,7 @@ app.use(
5663 }
5764 return callback ( new Error ( "Not allowed by CORS" ) ) ;
5865 } ,
59- } ) ,
66+ } )
6067) ;
6168
6269// Resolve the path to ../frontend relative to backend
@@ -107,7 +114,7 @@ app.get("/api/bonjour", (req, res) => {
107114 } ,
108115 ( service ) => {
109116 services . push ( service ) ;
110- } ,
117+ }
111118 ) ;
112119
113120 setTimeout ( ( ) => {
@@ -146,7 +153,7 @@ app.post("/api/update", async (req, res) => {
146153 {
147154 stdio : "ignore" ,
148155 detached : true ,
149- } ,
156+ }
150157 ) . unref ( ) ;
151158 } catch ( err ) {
152159 console . error ( err ) ;
0 commit comments