We will build an application where it will detect your location and get the temperature on this location. For example Dubai, temp is 82.00\n\nThese sites will help us to build our application: \n\nhttp://ipinfo.io/ – this will give us the location and return the record in object\n\nhttps://openweathermap.org/ – You need to signup and get the API key to use in our application.\n\nAnd we are going to start!\n

    \n

  1. Open the Terminal app on your mac and start by typing “npm init” and press enter
  2. \n

  3. Please enter the details on your app.
  4. \n

  5. You need the following in your application:\n
      \n

    • express
    • \n

    • ejs
    • \n

    • request
    • \n

    \nPlease install this npm package by entering this code

  6. \n

\n npm install express ejs request --sav\n\n4. Create a file and name it as app.js\n\n5. Add the code below and see my comment.\n

var request = require('request'); \nvar express = require('express'); \nvar app = express();\n//we will include the downloaded npm package in our application\n\nvar port = process.env.PORT || 3000; // setting up the port\nvar urlLoc = 'http://ipinfo.io/'; // \nvar city; \nvar weather;\n\n\n//the code below will get the details of our location in store in objects.\n  request({url: urlLoc,json:true}, function(err,response,body){\n  if(err){ // will check if there's an error\n    console.log('can\'t fetch location');\n  }else\n  {	// the object will be passed to the body arguments\n    city = body.city; // we will pass the data to the variable city.\n  }\n});\n  \n\n\n//this is the URL to get the weather value of our city and it will return in Objects, Please make sure you have the correct API key to make it work.\nvar urlWeather = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + '&units=imperial&APPID=APIKEYNUMBER';\n\n////the code below will get the details of our location in store in objects.\nrequest({url: urlWeather, json:true}, function(err,response,body){\n  if(err){\n    console.log('can\'t fetch location');\n  }else{\n    // the object will be passed to the body arguments\n      weather = body; // we will pass the data to the variable city.\n  }\n});\n\n//setting of our template to EJS \napp.set('view engine', 'ejs');\n\n//ROUTE \napp.get('/',function(req,res){\n  res.render('index',{city:city, weather:weather}); // we will pass the variables to our index.ejs page\n});\n//setting up port to connect on our application.\napp.listen(port);

\n6. Create a folder and name it as views and then open the folder.\n\n7. Create a file and name it as index.ejs\n\n8. Add the following code:\n

<h1>Check the temp of my City</h1>\n<%= city %>\n<%= weather %>

\n9. Save the index.ejs file and run the application.\n\n10. To run the application, please enter this:\n\nnode app.js\n\n11. Visit the website localhost:3000 by using any browser on your local machine.\n\n12. sample result: http://superbasicweatherapp.heroku.com/\n\nnote: the city will always be set to the server location. So this is not a good example of detecting the location. The request should be made by the viewer. Anyway, this is still a good start to give you a basic knowledge on how to use API.\nHappy Coding. ! :)\n\n \n\n \n\n \n\n