We now have a youtube channel. Subscribe!

Express.js | Hello World

Express.js | Hello World


Hello folks! Welcome back to a new edition of our tutorial on ExpressJS. In this tutorial, we are going to be discussing about how to create Hello World in ExpressJS.

We've set up the development, now it's time to start developing our first app making use of Express. Create a new file called index.js and type the following code in it.

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

app.get('/', function(req, res){
   res.send("Hello world!");
});

app.listen(3000);

Save the file, go to your terminal and input the following.

nodemon index.js

This is going to start the server. To test this app, open your web browser and then go to http://localhost:3000 and a message will be displayed as in the following screenshot.

hello_world

How the App Works?

The first line imports Express in our file, we have to access it using the variable Express. We make use of it in creating an application and assign it to var app.

app.get(route, callback)

This function instructs what to do when a get request at the given route is called. The callback function has two parameters which are, request(req) and response(res). The request object represents the HTTP request and has properties for the parameters, body, request query string, HTTP headers, and so on. Also, the response object represents the HTTP response that the Express app sends when it receives an HTTP request.

res.send()

This function takes an object as input and it sends this to the requesting client. Here we are sending the string "Hello World!".

app.listen(port, [host], [backlog], [callback]])

The app.listen function binds and listens for connections on the specified host and port. Port is the only required parameter here.

S.No.Argument & Description
1

port

A port number on which the server should accept incoming requests.

2

host

Name of the domain. You need to set it when you deploy your apps to the cloud.

3

backlog

The maximum number of queued pending connections. The default is 511.

4

callback

An asynchronous function that is called when the server starts listening for requests.



Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about Express.js Routing.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain