It is really important to have a separate configuration for all the hardcoded URLs having multiple instances across the project. It would help in testing and comparing projects like loklak with a separate configuration for hardcoded URLs. We would also be discussing deployment of Angular based loklak on heroku through this blog post.
Creating shared URL config
The idea here is to export a const Object containing all the collective hardcoded URLs used inside the project. We would try to store similar/collective URLs inside a same key e.g. all the github URLs must go inside key: ‘github’.
Storing URLs with a key instead of storing it inside a simple Array, makes it easier to add a new URL at required place and easy to modify the existing ones. Now, this configuration can easily be called inside any Component file.
Using config inside the component
Now, the work is really simple. We just need to import the configuration file inside the required Component and store/use directly the respective URLs.
First step would be to import the configuration file as follows:
Note: Respective path for url-config file for different components might differ.
Now, we would need to store the defaultUrlConfig inside a variable.
At this point, we have all the configuration URLs which could be extracted from configUrl.
Displaying URLs in HTML
We would use Angular’s interpolation binding syntax to display the URLs from configUrl in HTML. Let’s say we want to display FOSSASIA’s github url in HTML, then we would simply need to do:
This could be used as an example to to replace all the hardcoded URLs inside the project.
Deploying loklak search on Heroku
Note: I am skipping the initial steps of creating a project on heroku. It is very easy to setup a project on heroku, for initial steps please follow up here.
First step in this direction would be to add a server.js file in root directory and add the following express server code in it:
Second step would be to add the following commands inside package.json at respective attributes.
Click on the respective URL link inside the project UI to test the configuration for hardcoded URLs. To check the deployment on heroku, please open the following URLs:
Development branch: loklak-search-dev.herokuapp.com
Master branch: loklak-search.herokuapp.com