I received some advice about taking a look at my routes.
What does this mean and where should I put this code?
Is this the only code I need to change in the server.js file in Replit?
// A request to /api/:date?…
app.get("/api/timestamp/:timestamp", function(req, res) {
let timestamp = req.params.timestamp;
if(timestamp.match(/\d{5,}/)){
timestamp = +timestamp;
}
let date = new Date(timestamp);
if(date.toUTCString() == "Invalid Date"){
res.json({error: date.toUTCString()})
}
res.json({unix: dat.valueOf(), utc: date.toUTCString() });
});
app.get("/api/timestamp/", (req, res) => {
let date = new Date();
res.json({unix: date.valueOf(), utc: date.toUTCString() });
})
// I was told to take a look at my routes
// A request to /api/:date?…
// where do I put this code? Do I have other syntax errors?
Thank you for any help.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
The freeCodeCamp tests are looking for GET /api/:date? However, you currently have the following routes:
GET /api/timestamp/:timestamp
GET /api/timestamp/
To check if your code is working, freeCodeCamp tries to access your app and sees if it gets the right response. But it cannot do this if your routes have arbritary names. That is why it tells you how to name the routes, so that it can access them.
If you have your replit project running, click on the links under Example Usage. These links have to work in order to pass the challenge.
Note: the ? in /api/:date? simply means that the date parameter is optional. So, for example, both /api/2015-12-25 and /api should work.
Now you have two routes that are identical (/api/:date?), which doesn’t work. The easiest way is to change:
/api/timestamp/:timestamp to /api/:date
/api/timestamp/ to /api
You will have to change how you access the request parameter: let timestamp = req.params.timestamp will no longer work, so you have to change that to let timestamp = req.params.date
The rest of the code seems fine, except for one typo: