express Logging Write Express logs to a single file


First, install fs and path in your project

npm install --save fs path

Add the following code to your app.js file:

var express = require('express')
var fs = require('fs')
var morgan = require('morgan')
var path = require('path')

var app = express()

// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), {flags: 'a'})

// setup the logger
app.use(morgan('combined', {stream: accessLogStream}))

app.get('/', function (req, res) {
  res.send('hello, world!')

Now when you access your website you will see a access.log file was created in your project directory