πŸ“œ ⬆️ ⬇️

How to launch a simple static site in five β‰ˆ five-minute steps using Node.js and Express

As a response to the instruction β€œ How to make a simple web site in one hour, ” I decided to publish my own, consisting of five simple and foreseeable five-minute steps.

Step 1. Install the Node.js engine in accordance with the instructions available on the wiki for each of the supported operating systems. (For Windows, just download and run the MSI file, for CentOS, use the system package manager, and so on.)

Step 2. Create an empty directory and place in it the β€œ site.js ” file with the following content:
')
site.js
//  : var path = require('path'); var express = require('express'); //  : var staticSiteOptions = { portnum: 80, //   80 maxAge: 1000 * 60 * 15 //       }; //  : express().use(express.static( path.join(__dirname, 'static'), staticSiteOptions )).listen(staticSiteOptions.portnum); 

You can change the staticSiteOptions settings to your liking.

Step 3. In the same directory, issue the β€œ npm install express ” command , which will automatically create the β€œ node_modules ” subdirectory and install Express there by downloading from the Internet.

Step 4. In the same directory, create a β€œ static ” subdirectory and place the static site files in it. This subdirectory will be the site root. Files with the names β€œ index.html ” will be used as index files. In other words, when a server responds to a request with an address containing only the name of a certain directory (whether it is the site root or one of its subdirectories), a file with the name β€œ index.html ” from the specified directory (if any) will be displayed.

Step 5. In the same directory, issue the β€œ node site ” command , which will start the server. Convinced of its efficiency, ensure that this command is automatically run in this directory after each reboot of the operating system. (For example, on CentOS 6.2, you can use upstart for this purpose.)

Everything.

Under habrakat, it is appropriate to mention (and mention) that simply starting the server on some computer may not be enough: this computer should also have some name known to readers of the site, and requests sent to the external port that it listens should reach the server.

But the task of how to assign a name to the computer on which the server is running, I leave at the discretion of the reader, because it has several well-known solutions.

(You can buy a VPS with a fixed IP address, start a web server on it , then buy a domain, buy DNS servers or pick up free ones to taste, report their names to the domain trader, and then associate the IP address with the desired name in the primary control panel DNS server. You can run a web server on your home computer and buy a fixed IP address from an Internet provider. You can use a dynamic DNS, you can do without a fixed IP address. Or it may turn out that you don’t need to have an external IP address if a web server is needed for Intranet ).

And the task of how to forward through the firewall the port that the server listens to, I also leave at the discretion of the reader. Its solution is usually described in the firewall instructions.

Source: https://habr.com/ru/post/254393/


All Articles