Back to Node.js on IBM i for this one!
In this article, we will be creating a quick weather app running on our favourite server.
Once we have this up and running, in the next couple of articles we will be enhancing it to integrate with our existing applications already running on the IBM i.
At the end of this article, we should be able to open this new app, in a browser and enter a city, in the UK, and it will return the current weather for that city.
The figure below shows this result.
Where Do We Start?
Before we get down to coding, we need to subscribe to an API service that will provide weather details for us.
For this purpose, I will use the OpenWeatherMap API to provide weather information. You need to register for this service, but there is no charge. Details can be found at https://openweathermap.org/guide
Once we have registered, we will get an API key, which is required before we can take advantage of this service.
Creating our Framework
For this example, we will be using the NodeJS Express framework to give us a starting point.
In a PASE shell, we take these steps:
- Npm install –save express
- Npm install express-generator
- Express weather –view pug
- Cd into /weather
- Npm install
- Npm start
To test all is working, point our browser to http://your-ibmi:3000
Now we can start coding our new application.
We will replace the Index.js and index.pug files to suit our requirements. Let me start with the index.pug file, our application display file, for a better understanding.
The index.pug file will be used to ask the user to input a city in the UK and then display the results, so it will be used for both the GET and POST HTML operations.
The coding for this file is explained below.
That is all that is needed to ask the user to enter a city and return the weather results.
Next, we must change the index.js file.
This file will have two sections, the HTML GET, used for inputting the city, and a HTML POST to call the weather API and show the subsequent results.
The coding in the figure below, explains the steps we need.
Next, we move onto the steps we must complete to show the user the weather for the location they input.
In the index.js add a POST section.
As we used the existing index.js and index.pug files, there was no need to make any changes to the Express main router app.js.
Just point your browser to your IBM using port 4000 and away we go.
If you require a copy of the source I have generated for this application, it can be found on our GitHub repository. This can be found at https://github.com/AndyYouens/f_Weather
If you can make any enhancements to this, great, put in a pull request. That’s what open source is all about.
How easy was that to add a couple of lines to our Express framework and have a fully fledged weather application up and running.
Now we have our basic application, we will enhance it over the next few articles.
If you have any questions, either on this article, or any other open source, use the comments below, or send me a message on twitter @AndyYouens
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems with over 40 years IBM midrange experience.
k. obrien says