I found a lightweight web server solution in node.js, which is quick to install, quick to start up, and (relatively) simple to use. As a bonus, it will run on multiple operating systems.
Here are the steps I use to create a web server.
- Install node
- Create and open a folder for your pages
- npm Init to create the package.json file that node requires
- Create a "views" folder for your pages and add any pages to it
- Install express and ejs
- Create a main script
- Create HTML pages
- Start the web server
- Try it out
1. Install node
You can download node from https://nodejs.org/en/download/. Select your operating system and run the installer. It is a small download and a quick install.
2. Create and open a folder for your pages.
In Windows, I use the command prompt for this. I create my projects in either the c:\development folder (if I plan to keep them) or the c:\test folder (if they are scratch projects for learning)
So, for a scratch project, I would use the following commands to create a folder named "nodetest1":
3. npm Init to create the package.json file that node requires
Node requires a file named package.json with important information about the project. A quick way to create this is with the command "npm Init". It will prompt you for information required by package.json, even providing default values for some of these settings. Press ENTER at a prompt to accept the default in parentheses or override the default by typing in a new setting. Below is an example of me walking through this command, accepting mostly defaults
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install
save it as a dependency in the package.json file. See the session below for reference.
As you can see the file you create is echoed to the screen before you confirm its creation.
4. Create a "views" folder for your pages and add any pages to it
The "md" command works for this in the command prompt
5. Install express and ejs
Express and ejs are packages required for a simple web server. You can use the npm install command to install them. Add the "--save" switch to this command to update the dependencies in your package.json file. The following 2 commands install express and ejs packages, respectively.
Check out the package.json and verify that these packages now appear in the dependencies section, as shown in the example below:
6. Create a main script.
Below is the simplest script I could create that serves up any page in the "views" folder and sets a default page. In the example above, I named it "index.js". You can name it whatever you want, but you should set the "main" attribute in package.json to the name of your file. I have commented each line to explain what it does.
7. Create HTML pages
In the view folder, create some HTML pages. The script above specified "index.html", so we will need to create this one. Create any other page you like and name them with the ".htm" or ".html" extension. Here are 2 simple examples:
8. Start the web server
To start your web server, simply type "node" followed by the name of your main script. This will launch node and tell you the server is running on port 3000.
9. Try it out
Open a browser and type "http://localhost:3000/" in the address bar. You should see the default page (index.html) displayed. Here is the sample page rendered:
Type "http://localhost:3000/about.html" to see the "about" sample page described above. If you used my example, you will see the following:
At the command prompt, press CTRL+C at any time to stop the server, making these pages unavailable.
You can view this sample code at https://github.com/DavidGiard/simplenodewebserver.