We’re going to implement a configurable logger. You’ll be able to add or remove logging options based on your current needs.

Here are a couple of requirements that are expected of our new logger:

  • It should return a closure that could be used as a middleware.
  • It should serve a single purpose - log all incoming requests to the console.
  • The format of the logs should be configurable by the config string passed accepted as a parameter

Configuration details:

  • The config string can include any of the substrings :date, :method or :url.
  • These are the config options which should be split by space, i.e. :date :method or :date :url
  • For each known option you should include extra information into the console.log
  • Date should be printed out as a result of the function getFormattedDate
  • method and url should be picked from the function object req
    You’ll start by exporting a function logger from the file middleware.js.
    As we want to make it configurable, it shouldn’t be just a plain middleware handler
    Instead, it should return one!

If you’re not really sure what I just said, then I suggest going over the lecture on Advanced Functions once again.
And if you just need a hint, then make sure that logger returns a function that can be used as a middleware and calls next() in the end of its execution

This task is part of the Full-Stack JavaScript Course.
If you have any issues with it, you can ask for community help below the post.
Feel free to help others if you’ve already solved the task.

functions.js

export const getFormattedDate = (date) => {
  if (date) {
    return date.toUTCString();
  }
  return new Date().toUTCString();
}

index.js

import { server } from './server.js';

const port = 8080;

server.listen(port, () => {
  console.log(`Server is running on ${port}`);
});

server.js

import express from 'express';

const server = express();

server.get('/', (req, res) => {
  res.send('Learning to use middleware!');
});

server.get('/about', (req, res) => {
  res.send(`I'm going to become a JS Developer in ${new Date().getFullYear()}!`);
})

export { server };