Could someone please explain why app.use(cors()) (in the previous exercise) requires the “()” after cors whereas app.use(’/data/:index’, indexExists) doesn’t use “()” like: “…indexExists())”
I’m sure this is basic and I’ve probably read about it plenty of times but my memory is sieve-like!
Not sure if I understand your question. However, () as in parentheses is used with all functions whether they receive parameters or not. Functions - MDN
I’m still unsure as to why CORS middleware package is passed to app.use(…) with “()”, as in: app.use(cors());
In the previous exercises middleware functions are passed like regular callback functions, with no “()”.
Examples being:
app.use(moodleware);
app.use(timeMiddleware);
app.use(errorHandler);
I understand (vaguely) that this is because they are referenced here rather than being invoked immediately. I thought that this would also be the case when passing “cors”, but I’m very much a noob and am frequently caught out by these simple things.
If that is the case then yes you would omit the () to reference it in with something like console.log. Perhaps you would want to do that to make sure you are returning the expected result before you move on and return some kind of value/s. You can test that out with other more simple things in the console.