It’s time to introduce the first part of my project – a microservice for downloading weather data from a weather API provider. In this post I will explain my setup and provide steps for running my project if anyone would like to play with it. The folder structure may be a little bit overkill, because I’ve recently created a much more complicated solution at work, which requires for example dealing with different environments (testing/staging/production). I didn’t want to just leave one or two scripts since I kind of like the way I have it organized, so I stick to it. I noticed that there are many beginner developers taking part in GetNoticed! contest, some of them even having their first touch with programming, so I will try to explain everything that may be confusing. So do not frown with compassion when I’ll write about something obvious for you. ;)
Running my project
I’m sorry Linux and OSX fans, but I’m using Windows, so it’ll work only on Windows for now. As far as I’m concerned the difference can be in defining chained commands in the npm scripts (start and setup).
If you want to play around with my microservice project, you need to do following steps:
1. Download the Node.js from here
I like Windows installer as it also install the Node Package Manager (npm) and adds both node and npm to PATH, which makes it possible to use them from command line from any directory.
2. Clone the repository
In the command line run: git clone https://github.com/Harriet92/AmbientWeather.git
3. Navigate to the ApiServer directory
4. Open a command line there (if you’re not already using it)
If you have the directory opened you can click right button when holding shift and select “Open in command line” option.
5. Type “npm run setup”
This will do several things:
• install npm packages – both the ones used in the source code and also the ones that are needed for example for downloading typings packages or running buil scripts (gulp). The script knows which packages it should download thanks to the package.json file.
• install typings packages – accordingly to the typings.json file.
6. Type “npm start”
It will also do several things:
• “compile” (we all now know that it’s in fact transpiling) the project and save the result under the “dist” directory. For compiling I use gulp. Again, it’s a bit of overkill for now, since in fact we only need to invoke the “tsc” – Type Script Compiler.
• run the program by invoking “node ./dist/index.js” command. Index.js is our “Main” file.
If there are no errors in the console, the server is probably working. :)
You can check this by navigating to your browser and typing “localhost:4000/healthCheck”. You should see the “Server running” text.
Dive inside the source code
At the moment of writing this post, there is nothing more in the repo than basic configuration. So lets see what had to happen to display that message for us.
Right now I will only use the Test configuration, but eventually the right one would be chosen depending on the NODE_ENV variable. It can contain such information as our app port, db connection string or logs directory. More secure solution would be for example reading the DB config from the environment variable. In case of a hacker attack, if someone somehow got file reading privileges, he or she would get to know the db connection credentials. In this file we’re defining the configs, in the next one we’re injecting the one that is used right now.
The next file is just an enum for environments. The next one, GlobalParameters will contain information about the NOVE_ENV variable or for example information about the OS that the server is running on (eg. for setting up logs).
Middlewares are programs that catch the request, do something with it and pass it forward. I’m using Express as a web application framework, so setting them up is really easy. Right now I’m only using body-parser, to get my requests translated to json. Thanks to that, when I get my request parameter in handlers, it already has a neat “body” property that I can easily read. The MiddlewareBase file is also a place where I set up the base, common routes, as for now only the health check route (which is very important btw, for setting up downtime alarms, for example using NewRelic). The CommonRoutes and HealthCheckRoute files contain detailed routes tree setup.
Running the app
Last, but not least – our Main file. It just runs Node.js app.
You probably noticed this line of code
It has to be added at the begging of every file for the TypeScript to know where to look for typings definitions. If you have some strange error messages, saying that some package is not installed when in fact they are, there is a possibility you forgot about that. :)
That’s it for now. There are many crucial things missing right now – for example logs set up or data access, but I want to have my thermometer up and running as soon as I can. The next step will be downloading weather data from API and storing it in the Redis database.
And as always - like my fan page or follow me on Twitter to get notified when future posts appear. (｡◕‿◕｡)comments powered by Disqus