Source on GitHub
Introduction
Not every time you host a web site you need a server side logic. When all you need is just static web pages hosting using something that just hosts your site, and does nothing else, can do the work for you.
Background
Many web sites today are written as single page applications. Usually, those sites don't need a traditional server side, the client performs some http requests to some services, maybe even microservices, just to get the relevant data. The rest of the work is performed in the browser. In such a case, hosting static web pages is enough.
In this article, I want to introduce an application that I wrote (HostMe
) that does exactly this.
All you need to do to host static pages with HostMe
is just to run a single EXE file (no any kind of installation is required).
Using HostMe
- Download HostMe.
- Put HostMe.exe in the same directory where your index.html file is found:
- Run HostMe.exe as administrator (The default hosting port is on port 80, in order to host on port 80, you need to run as administrator. If you would like to change the default port, look here):
- Now you can open your browser and type localhost:
You can see that all site files were loaded perfectly, even the javascript code. If you want to try it by yourself, you can download this example.
Pay attention that you don't need to restart HostMe at any point, any addition of new file or change to existing file will be effected immediately.
Run HostMe as a Windows Service
Run the cmd as administrator (in order to install Windows service, you must be administrator), go to the folder where HostMe.exe is found and type:
HostMe.exe -install
Now you have a Windows service running that will host your site and start automatically on every system boot. You can see it in the services.msc:
You can enter localhost again in the browser, and it works like a charm:
If you want to uninstall the service, all you need to do is this:
HostMe.exe -uninstall
You even don't need to stop the Windows service first.
Logs
HostMe
writes to log files every request it gets. You can see it in the logs folder that will be created in the same directory as the EXE.
Please pay attention that since the logs folder is found in the same directory as HostMe.exe and you use the default configuration, any user can access your logs files. If you don't want this to be possible, change the root directory in the site as explained in the next section.
The default configuration of HostMe
is to run on port 80 and to host what is found in the same directory where the EXE is found.
In order to change this behavior, create a new json file called HostMe.config.json in the same directory as the EXE. The file's content should be in this format:
{
"Path": "C:\MySite\siteRoot",
"Port": 8080
}
The path is the path to the root folder of the site. It can be either relative to the EXE or absolute.
The port is the port on which the site will be hosted.
If you omit one of the properties, it will get its default value (the port will be 80 and the root folder will be the folder of the EXE).
So, if you just want to change the root folder of the site to siteRoot
, and your EXE is found in C:\MySite, you can write this:
{
"Path": "C:\MySite\siteRoot"
}
or that:
{
"Path": "siteRoot"
}
What's Next?
The code of HostMe
is actually an open source and can be found here.
If you want some explanations about the code, I will be glad to write another article with explanations.
Feel free to write any suggestions or issues you have found.
Useful Links