Basic Node and Express - Chain Middleware to Create a Time Server

Tell us what’s happening:

  1. The /now endpoint should return the current time.

let express = require(‘express’);
let app = express();
require(‘dotenv’).config()

//4#
app.use(“/public”, express.static(__dirname + ‘/public’));

//7#
app.use((req, res, next) => {
console.log(${req.method} ${req.path} - ${req.ip});
next()
})

//3#
app.get(“/”, (req, res) => {
res.sendFile(__dirname + “/views/index.html”);
})
//#5
// app.get(“/json”, (req, res) => {
// res.json({“message”: “Hello json”});
// })

//#6

###Your project link(s)

solution: https://3000-freecodecam-boilerplate-zuxg16a5qip.ws-eu116.gitpod.io

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0

Challenge Information:

Basic Node and Express - Chain Middleware to Create a Time Server

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

You did not create a /now GET route.

app.get("/somePath", theMiddlewareHandler, theFinalHandler);
  • The middleware handler should take req, res, next and set req.time to the current time as required. Then call next to pass control to the next handler in the chain.

  • The final handler should send, as JSON {time: req.time}, with req.time being what the middleware handler set it to be.

Basic code structure needed:

app.get(
  "/test",
  (req, res, next) => {
    req.test = "test";
    next();
  },
  (req, res) => {
    res.json({ test: req.test });
  }
);