Timestamp Microservice tests won't pass

This is the project: https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/timestamp-microservice

What’s the problem?

I can’t get a single test to pass even though I’m pretty sure they all should be (except for the invalid date one which I haven’t handled yet)

I’m 99% confident that at least one test should be passing.

Specifically the one described as:

A request to /api/1451001600000 should return { unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }

Here’s a url to my app with that exact input: https://fcc-microservice-project-2.herokuapp.com/api/1451001600000

You should see the exact output described in the test example which is:

{
  unix: 1451001600000,
  utc: "Fri, 25 Dec 2015 00:00:00 GMT"
}

You can see the same JSON response returned from the demo application here: https://timestamp-microservice.freecodecamp.rocks/api/1451001600000

What have I tried?

I honestly don’t know what to try. The responses are exactly the same given exactly the same input.

I checked the logs in heroku for this too and I can see the requests coming in with the input and my app is returning the expected output.

Please help! And thank you!

Huh, weird. Do you have a link to your source?

Yeah sorry for the late response. Been out of the house.

Repo: https://github.com/DanJFletcher/fcc-timestamp-microservice

The app.js file is:

var express = require('express')
var app = express()

const getTimeStamp = (req, res) => {
    let date

    if (!req.params.date) {
        date = new Date()
        const taco = { 
            unix: date.getTime(), 
            utc: date.toUTCString() 
        }
        console.log(taco)
        return res.json(taco)
    }

    date = !isNaN(req.params.date)
        ? new Date(Number(req.params.date))
        : new Date(req.params.date)

    const timeStamp = {
        unix: date.getTime(),
        utc: date.toUTCString()
    }
    console.log(timeStamp)

    res.json(timeStamp)
};

app.get('/api/:date?', getTimeStamp)

module.exports = {
    getTimeStamp,
    app
}

Thanks!

If you look at the network tab on the submit page you can see it is getting CORS blocked. You can add cors to app.js, when I do that I pass all but the one test (invalid date).


Usually, if you want separate route modules you would use router and import and use them in the server.

Something like this (remove most code for brevity):

app.js

var express = require('express')
var app = express()
var router = express.Router();

// getTimeStamp controller code removed for brevity

router.get('/:date?', getTimeStamp)

module.exports = router;

server.js

var express = require('express');
const app = express()

var cors = require('cors');
app.use(cors({optionsSuccessStatus: 200}));

app.use('/api', require('./app.js'))
1 Like

Ohh I didn’t even think of that! Thank you!

I thought that just exporting the app and using it in the server.js was enough for the CORS config to work.

Basically I did this but I guess this isn’t it?

var { app } = require('./app.js')

// 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})); 

Just trying your strategy out now and I’ll see if this works.

Yep! That was it!

Thank you :slight_smile:

Obviously I need to understand the difference between registering routes on the app vs the router better. Going to spend some time reading into that more closely.

1 Like

Ahh I think I understand now.

I kinda want to put this here incase someone else comes across this.

I think it comes down to this:

app.get('/api', someFunction);
var cors = require('cors');
app.use(cors({optionsSuccessStatus: 200})); 

The order of registering things on the app instance matters here. So CORS isn’t applied to the route.

If you were to set this up in one file you would want the order like this:

var cors = require('cors');
app.use(cors({optionsSuccessStatus: 200})); 
app.get('/api', someFunction);

But since I was registering my route in another file and then exporting the app, essentially I was registering things in the order from the first code snippet which didn’t register CORS with my route.

So the way you split up routes and organize them while still being able to register them in the order you need is to use the express.Router which sets your route handlers up without actually registering them. You register them later using app.use('/api', yourRouter)

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