Hello World

In this step, you will create a basic Express.js application that responds with “Hello, World!”

Express.js is a popular web server framework for Node.js.  To learn more about Express.js, you can check out their official documentation here

Create the Node.js Application

First, create a new directory to host your application:

mkdir nodeserver

cd nodeserver

Next, initialize your application with npm init

npm init -y

npm init is a built in command for the npm cli that allows a user to quickly scaffold a new Node.js application.  Using the -y flag will accept the sensible defaults.  To learn more about this command, see the official npm documentation

Then install the Express.js node module:

npm install express

Now, let’s start creating our server.  Create a file named server.js in the root of your application and add the code below to produce an Express.js server that responds on the "/" route with "Hello, World!".

const express = require('express');
const PORT = process.env.PORT || 3000;

const app = express();

app.get('/', (req, res) => {
   res.send('Hello, World!');
});

app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

To start the application, run the following command:

npm start

Once the application is started, you should see a similar output in your console:

Server listening on port 3000

Navigate to http://localhost:3000 and you should see the server respond with 'Hello, World!'. You can stop your server by entering CTRL + C in your terminal window.