First of all, i am very much new to node.js and web services.
I am working on something which requires me to get the temperature from openweather.org API and serve the result in a HTTP.
The api is
http://api.openweathermap.org/data/2.1/find/name?q=London
And I would like to save the city name, temperature and degree in an HTML in the following format:
<!DOCTYPE html>
<html>
<head>
<title>Something</title>
</head>
<body>
<h1>Current Weather</h1>
<hr/>
<strong id="city">{{city}}</strong>
<span id="temperature">{{temperature}}</span>
<span id="unit">{{unit}}</span>
<hr/>
<small>last update: <span id="lastupdate">{{datetime}}</span></small>
</body>
</html>
How can I do that? any help.
step 1: create a simple node app to perform the api request and return the output to the console. Use request to make your life easy
step 2: use a template engine like the one in underscore or others to merge api output with above html template. For example:
var apiResult = JSON.parse(the_result_of_api_call);
var template = '<strong id="city"><%= list[0].name %></strong>';
var output = _.template(template)(apiResult);
step 3: create a node express website to combine the above and do a response.send(mergedoutput).
Visit http.request for more information.
var options = {
host: url,
port: 80,
path: '/resource?id=foo&bar=baz',
method: 'POST'
};
http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();
Above block of code will help you retrieve data from other website.
You will need to have a look at backbone.js and underscore.js if you want to render data in a (similar)fashion which is mentioned in the question.
You can see one example which has similar stuff : backbone.js with node.js