So far the course ran the shop for you. On a server, a command starts it. With Node.js it needs no extra package: the program node runs your file, and the web server built into Node.js, the one Express uses underneath, is made for real traffic.
node runs the file from its first line to its last. If nothing more happens, the file ends and the program ends with it. So the file starts the web server itself: app.listen(port) makes Node.js listen on that port and keeps the program running. For each request that arrives, Node.js calls app(req, res), and Express finds the route. With only a port, Node.js listens on every network interface, so other machines can connect.
A NAME=value written before the program sets an environment variable for it, the kind process.env reads. For example, a blog in the file blog.js, which ends with site.listen(3000); and should use the Paris time zone, could start with:
TZ=Europe/Paris node blog.js
One variable matters for the shop. Without NODE_ENV=production, Express runs in development mode, where a route that crashes answers with the error and the names of your files: made for you, not for visitors. With the full command and a file that listens, the check prints:
$ NODE_ENV=production node app.js
NODE_ENV=production: Express runs in production mode, so when a route crashes the visitor gets only "Internal Server Error"
node: runs your file; the web server built into Node.js, the one under Express, is made for real traffic
app.js: node runs the file from its first line to its last
app.listen(8000): listens on port 8000 of every network interface, so other machines can connect
For each request, Node.js calls app(req, res):
GET / -> 200 OK: The shop is open
Your task: the shop's app and its route are already written. Build the string command from an array with one string per part: NODE_ENV=production, then node, then app.js. In the file, below the route, read the port from PORT as a number, with the default "8000", into port, and make the app listen on it.
Assignment
Follow the Coding Tutorial to write the command line that starts the shop on the server, and to make the file listen for requests.
Hint
The variable goes before the program, with no spaces around =. The file's name is written in full, with its .js. The port is read the way the settings lesson did it.
Free plan: 30 tutor messages. PRO is unlimited.
