Still says i failed

My app satisfies all the requirements but still it fails me on every requirement.

// server.js
// where your node app starts
var express = require('express');
var app = express();
var bparser = require('body-parser');
var moment = require('moment');
var dandt = require('date-and-time');
var express = require('express');
var app = express();

app.use(function(req, res, next) {
  console.log(`${req.method} ${req.path} - ${req.ip} `);
  next();
})
app.get('/api/:date?', function(req, res, next) {
  const dd = Date.parse(req.params.date);
  const d = new Date(req.params.date);
  const dutc = d.toUTCString()
  console.log(req.params.date);
  console.log(d.toUTCString());
  console.log(dd);
  const unixnum = Number(req.params.date);
  console.log(isNaN(unixnum));
   if (isNaN(unixnum) === false){
    let unixxnum = new Date(unixnum);
    return res.json({unix:unixnum,utc:unixxnum.toUTCString()});
    console.log(unixxnum.toUTCString());
  }  else if (req.params.date === undefined ) {
    let curr = new Date();
    return res.json({ unix: Date.parse(curr), utc: curr.toUTCString() });
   
  }else if (dutc === 'Invalid Date') {
    return res.json({ error: 'Invalid Date' })
  }
  else {
    return res.json({ unix: dd, utc: dutc })
  }

})
// init project

// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC 
var cors = require('cors');
app.use(cors({ optionsSuccessStatus: 200 }));  // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function(req, res, next) {
  res.sendFile(__dirname + '/views/index.html');

});


// your first API endpoint... 
app.get("/api/hello", function(req, res, next) {
  res.json({ greeting: 'hello API' });

});



// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

Your project link(s)

solution: https://replit.com/@person654123/boilerplate-project-timestamp

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0

Challenge: Timestamp Microservice

Link to the challenge:

I can’t get yours to run. When I try to submit it, I see 502 errors. I can’t even get it run on replit. I notice that you have:

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

That might cause problems.

Get an app that runs and we can try to help you.

Move the CORS middleware usage all the way up to the top (right after your other require statements). It has to be mounted before the routes.

It didn’t run at first until I forked it so I’m not sure what is up with that.

1 Like

Thanks guys. I deleted the extra var express = require('express'); var app = express(); and moved my code all the way down and it worked.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.