Creating a “hello world” Node.js Application

First, we will start by creating a directory for our project and then install some dependencies for our simple “Hello world” app using the command line.

mkdir nodejs-server

cd nodejs-server

**Install npm And Express Framework: **
Install npm and Express, which is a Node.js framework.

Then, initialize npm in our directory.
Copy or use the command below in your command line or terminal.

npm init

Above, npm creates a package.json that holds the dependencies of the app.

**Next, **install the Express framework dependency.

npm install express --save

Codebase should look like this below:

Create a package.json file with the following content:

{
  "name": "node-app",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "license": "MIT",
  "dependencies": {
    "express": "^4.18.2"
  }
}

Create an app.js file with an HTTP server that will return Hello world.

Here is the code below :

const express = require('express')
const app = express()

// This tells the app which port to listen to
app.listen(process.env.PORT || 5000, () => {
  console.log(`Server is running on port`)
})

//This shows the response that will sent to the user
app.get('/', (req, res) => {
  res.send('Hello World')
})

Now, Save this file above, open your terminal, CLI (Command Line) to run the application

node app.js

The app is now ready to launch:

It will show you on your terminal Server is running on port 5000

Go to your browser, copy and paste ** http://localhost:8080/ ** in your browser to view it.

Here is a “Hello world” output on my browser :