How Do You Write a Hello World Program in Node.js?

Problem scenario
You want to write a "Hello World" program using Node.js.  What do you do?

Identify the external IP address of the server (e.g., run "curl" if you have access to the internet).

1.  Install node and npm on a Linux server.  If you need assistance with this, see this posting.
2.  Run these two commands:
mkdir contint
cd contint

3.  Run this command:

npm init  # respond to the interactive prompts.  You probably will want to accept the defaults, but for the "Entry point" one with the suggestion of "index.js" change it to "contint.js"

For "Is it ok?" type "yes" (with no quotes) and press "Enter".

4.  Create a file called contint.js and have the following as content:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World');

app.listen(2987, function () {
  console.log('Example webpage application listening on port 2987!');

5.  Run these two commands:

npm install express --save
node contint.js

6.  Open a web browser on your workstation.  Go to x.x.x.x:2987 where x.x.x.x is the external IP address of the Linux server.

Leave a comment

Your email address will not be published. Required fields are marked *