JavaScript: How to use Node.js

JavaScript: How to use Node.js

Overview

This article will explain how to run JavaScript files with Node.js. Node.js is a JavaScript runtime environment.

Procedure

  • Go to the official Node.js (1) website and download the software. There are different installation files for different operating systems.

  • Create a JavaScript file with the content shown below

const http = require('node:http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

What the JavaScript File Means

  • "const http = require('node:http')" - "Require" loads modules or other files that contain JavaScript code to run them. By specifying 'node:http' in parenthesis, the node framework will be used to run the Javascript files.

  • "const hostname = 127.0.0.1" - When running a Node.js application, by default it runs on "127.0.0.1" or localhost. The word localhost is shown in the url bar when the app is active.

  • "const port = 3000" - The application can be run on any port which is specified by this line of code. By default, a Node.js app runs on port 3000, and in the URL bar, it will show as "localhost:specified_port_number".

  • "const server = ....." - This line of code first creates the http server. req and res means request and response respectively. The function first accepts a request and then sends data or a response. Many three-digit numbers can specify the response of the code, and successful ones are 200.

  • "res.statusCode = 200" - Specify the successful response code in order to display data after the conditions to run Node is met.

  • "res.setHeader('Content-Type', 'text/plain')" - Most content-type for webpages or files are "text/plain" or UTF-8, which is why 'Content-Type', 'text/plain' is specified.

  • "res.end("Hello, World!\n');" - Write "Hello, World" will be displayed when Node.js is running.

  • "server.listen(port, hostname, () ....." - Run the JavaScript file is ran in the Terminal successfully, a message will appear: "Server running at 127.0.0.1:port_number"

Running Node.js

In the terminal, write "node" and then the name of the JavaScript file it is saved as. The ".js" extension does not have to be written:

node name_of_file

Running Node.js with Exports

A JavaScript file that uses Node.js can read other JavaScript files by exporting and importing functions. Two JavaScript files are needed, one file with the function that will be exported and another that runs Node that will read the exported function.

  • Create a JavaScript file that has a function. The function can be named anything, and the name is needed to be referenced to call the function in the JavaScript file that runs Node.js.
// name of file - export_module.js

exports.insert_name_here = function () {
    return "Hello, good job exporting the function!";
};
  • Now create a JavaScript file with Node.js. Below in the script, has comments that shows the differences from the first Node.js script. Those differences are having another line of code that uses "const" that uses the "require" keyword to read the name of the file that has the function.
const http = require('node:http');
// Add line of code below to read module, 'export_module is the name of the file shown above'
const import_module = require('./export_module');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  // Include res.write() downbelow, write the name of the variable that imported the other JavaScript code
  // followed by a dot, followed by the name of the function in the other file
  res.write(import_module.insert_name_here())
  res.end();
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Sources

https://nodejs.org/en (1)

https://nodejs.org/docs/latest/api/synopsis.html (2)