They did it so as to transport commodities from the continent to the seaports.
The two regions had dreadful roads and it was extremely difficult to transport commodities to seaports which would then be taken to factories worldwide. They needed railroads to move those commodities since they usually came in high amounts so they built railroads all across the continent out of their own need.