I have created a very simple API based on two files as follows:
api.js
import express from 'express';
const app = express();
import { getWhereToApi} from './lib/handlers.js';
const port = 3000;
app.get('/api/vacations', getWhereToApi);
app.listen(port, function() {
console.log(`Express started in ${app.get('env')} ` +
`mode on http://127.0.0.1:${port}` +
`; press Ctrl-C to terminate.`)});
handlers.js
var whereTo = {
"GRU": {
"destinations": ["MAD", "LAX", "MEX"],},
"LAX": {
"destinations": ["NYC", "GRU", "MEX"],},
"NYC": {
"destinations": ["LAX"],},
}
export async function getWhereToApi(req, res, iata){
res.json(whereTo[iata]);
}
I want to be able to pass the IATA as var somehow (e.g. "GRU"), so I would get the following result:
{
"destinations": [
"MAD",
"LAX",
"MEX"
]
}
How can I do it? Thank you!
iatavalue to come from? The way you have it configured now, you're expecting Express to provide it when it calls the route handler, but that isn't going to happen the way you show it. Where is it supposed to come from? Do you want it to be in the URL? If so, show how you want it in the URL?