-
Notifications
You must be signed in to change notification settings - Fork 24
Quick Start: Implement a Controller
-
Now we can go ahead and implement the actual controller by creating a file called
weather.js
in theapi/controllers
folder. -
The
weather.js
file should contain a function that will take in request and response objects, query the Open Weather Map API using the city query parameter and return the current weather conditions. Open Weather returns a JSON object. Finally, we also need to export this function so that it is available to the outside world. We will use therequest
library to make the request. So, add it topackage.json
"dependencies": { "request": "" },
The
getWeatherByCity
function should look as follows:'use strict'; var util = require('util'); var request = require('request'); module.exports = { getWeatherByCity: getWeatherByCity } function getWeatherByCity(req, res) { var city = req.swagger.params.city.value; var url = "http://api.openweathermap.org/data/2.5/weather?q="+city+"&units=imperial"; console.log('Executing request: '+url); request.get(url).pipe(res); };
-
Try running the program by executing
a127 project start
and use the following curl command:curl http://localhost
Having Trouble? Try posting your question to the Apigee Community. Or, for more links and resources, check out our Help Page
Need help? Visit the Apigee Community ! |
---|
-
Getting started
-
Add policies to your API
-
Add security policies
-
Deploy your projects
-
Programmatic hooks
-
Good to know about
-
Deep dives
-
Reference topics
-
Troubleshooting and getting help
-
Related resources