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'))
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.
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.
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)