Basic Node and Express - Chain Middleware to Create a Time Server- First test not passing

Tell us what’s happening:
The first test is not passing

“The /now endpoint should have mounted middleware”

Your code so far

var myApp = require('./myApp');
var express = require('express');
var app = express();

app.use(function(req, res, next){
  console.log(req.method + " " + req.path + " - " + req.ip);
  next();
})

if (!process.env.DISABLE_XORIGIN) {
  app.use(function(req, res, next) {
    var allowedOrigins = ['https://narrow-plane.gomix.me', 'https://www.freecodecamp.com'];
    var origin = req.headers.origin || '*';
    if(!process.env.XORIG_RESTRICT || allowedOrigins.indexOf(origin) > -1){
         console.log(origin);
         res.setHeader('Access-Control-Allow-Origin', origin);
         res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    }
    next();
  });
}
app.get('/', function(req, res) {
  res.sendFile(__dirname + "/views/index.html");
})
app.get('/json',function(req,res){
   if (process.env.MESSAGE_STYLE === "uppercase"){
    res.json(
      {"message":"HELLO JSON"}
      )
    } else {
      res.json(
        {"message":"Hello json"        }
      )
    }
    })
app.use(express.static(__dirname +"/public"));
app.use(__dirname +"/public", express.static)

function currentTime() {
  return new Date().toString();
}

app.get('/now', function(req, res, next){
  req.time = currentTime();
  next();
}, function(req, res){
  res.json({time: req.time});
})

// app.get("/",function(req, res) {
//   res.send('Hello Express');
// })
var port = process.env.PORT || 3000;
bGround.setupBackgroundApp(app, myApp, __dirname).listen(port, function(){
  bGround.log('Node is listening on port '+ port + '...')
});

Your browser information:

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

Challenge: Chain Middleware to Create a Time Server

Link to the challenge:

After a couple of days of seeking help everywhere, got some help from freeCodeCamp’s discord channel.

Apparently, all the code that I was writing in server.js file was obstructing the testing and that triggered the issue of not passing the test.

Moved all my code from server.js to myApp.js and the test passed finally. All thanks to the super helpful people

freeCodeCamp’s discord channel.

1 Like