Categories
JavaScript Answers

How to log Node Express JavaScript to a output file?

Spread the love

To log Node Express JavaScript to a output file, we use the express.logger method.

For instance, we write

const logFile = fs.createWriteStream("./myLogFile.log", { flags: "a" });

app.use(express.logger({ stream: logFile }));

to create the logFile write stream with createWriteStream.

We call createWriteStream with the log file path and an object with flag set to 'a' for append.

Then we call express.logger to with an object with stream set to logFile to return a middleware that we use in our app to add logging to the file.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.