Express JS Hello World

Install NodeJS and NPM

First thing we need to do is prepare an environnement for Express to run. We are going to install NodeJS and follow the installation instructions.

Prepare a workspace and start a project

Once we have instal…



Install NodeJS and NPM

First thing we need to do is prepare an environnement for Express to run. We are going to install NodeJS and follow the installation instructions.
Web capture_7-5-2021_2217_nodejs.org



Prepare a workspace and start a project

Once we have installed Node.js and Node Package Manager (NPM) on our machine, let’s open up a terminal window (or CMD, on Windows) in the desired folder or use the following command to create a folder and use it as a workspace:

$ mkdir ./hello_world
$ cd ./hello_world

Now, we are ready to start our first application, to do that, type in the following command:

$ npm init -y

What it basically does is create a file named package.json that contains all the information about our project and its dependencies.



Install Express

Next, we need to install express module using NPM via the command:

$ npm install express --save

Now all we need is to create our main script, we’ll be naming it index.js since that’s the default name (other conventional names can be app.js or server.js).

We can create a new empty file from the terminal using the following command:

touch index.js

Let’s open our newly created file in any IDE or text editor (Notepad, Notepad++, Atom …) but I would recommend using a sophisticated IDE like VS Code and let’s type in the following lines in order:

const express = require('express');

The first line would tell our app to import the module we are using (express).

const app = express();

This second line let us define express as a function

After that we need to define something called “a route” to the root of our website that will let us send an HTTP request to our server and GET a response that says Hello World!:

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

Last thing we need to do is instruct our app to be listening on a port, for example port 3000:

app.listen(3000)



Run the server app

Now let’s head back to our terminal window and type in the following command which will compile our code and start our server.

$ node ./index.js



Check if it works

Finally, we can load http://localhost:3000/ in a browser to see the result.

Hello World Screenshot


Print Share Comment Cite Upload Translate
APA
Youssef Allali | Sciencx (2024-03-29T08:59:14+00:00) » Express JS Hello World. Retrieved from https://www.scien.cx/2021/05/07/express-js-hello-world/.
MLA
" » Express JS Hello World." Youssef Allali | Sciencx - Friday May 7, 2021, https://www.scien.cx/2021/05/07/express-js-hello-world/
HARVARD
Youssef Allali | Sciencx Friday May 7, 2021 » Express JS Hello World., viewed 2024-03-29T08:59:14+00:00,<https://www.scien.cx/2021/05/07/express-js-hello-world/>
VANCOUVER
Youssef Allali | Sciencx - » Express JS Hello World. [Internet]. [Accessed 2024-03-29T08:59:14+00:00]. Available from: https://www.scien.cx/2021/05/07/express-js-hello-world/
CHICAGO
" » Express JS Hello World." Youssef Allali | Sciencx - Accessed 2024-03-29T08:59:14+00:00. https://www.scien.cx/2021/05/07/express-js-hello-world/
IEEE
" » Express JS Hello World." Youssef Allali | Sciencx [Online]. Available: https://www.scien.cx/2021/05/07/express-js-hello-world/. [Accessed: 2024-03-29T08:59:14+00:00]
rf:citation
» Express JS Hello World | Youssef Allali | Sciencx | https://www.scien.cx/2021/05/07/express-js-hello-world/ | 2024-03-29T08:59:14+00:00
https://github.com/addpipe/simple-recorderjs-demo