How To Create Your First Express.js Web Application?
Tags: programming, express, node, beginners
Published: 2021-04-12 07:20:25

---
title: How To Create Your First Express.js Web Application?
published: true
tags: programming,expressjs,nodejs,beginners
canonical_url: https://gillesheinesch.medium.com/how-to-create-your-first-express-js-web-application-c46cd61dda0f
---

More and more web applications are using ExpressJS. It’s an easy-to-use and powerful web framework for Node.js. What we’re learning today is how to set up a basic Express.js website.

#### Prerequisites:

- Node.js with NPM installed on your system

### Let’s start!

First of all, create a new project folder. You can do this manually or with the following commands:

{% gist https://gist.github.com/gillesheinesch/529fc532d924a706e587002884477d51 %}

If you’ve done, you should create a package.json using the command npm init in the console. If everything has worked fine, a new file should have been created called package.json . In this file, all project settings are saved like all dependencies the project needs, what the main file is, … You can read more about what the package.json is [here](https://blog.ezekielekunola.com/understanding-the-package.json-file).

Now it’s time to install express.js. You can be doing this with the following command: npm install express --save With this command you install expressjs in your project folder and save it into your package.json .

As in nearly every beginner tutorial there is a “Hello World” example that we’ll create now. Create a new file in your root folder (In this example it should be in ./myNewApp) called app.js . In this file we’ll setup express.js.

If you’ve successfully created this file, you can start entering the following code:

{% gist https://gist.github.com/gillesheinesch/9e344c613328a31fc9055a39a3b9aa23 %}

In the first line we use require() which lets Node.js know that we need express for this file to work. In the second line we create a new web server using the express() function. In the third line we set the port which can be whatever you want.

You can start your web application using the command node app.js . As you can notice, there is no output in the console. This means you have no response if the web application is running now or not. There’s an easy solution for that. Just add the following lines below your code from before:

{% gist https://gist.github.com/gillesheinesch/a7bfa577113a63112e133a59c1387bfb %}

If you restart your express server now, you will see that in the console is written _“My first Express.js web application listening at http://localhost:80”_

Now your web server is running on port 80 but you don’t have any routes yet. Add the following 3 new lines to your code in your app.js :

{% gist https://gist.github.com/gillesheinesch/404ef617bcd85a099ca36fc7c16adec3 %}

In the first line we define that the web application should listen to every **GET** request which is coming from / . This means that if you access your website locally using localhost:80/ the web application will listen to it and execute the code which is on line 2. On line 2 we define that whenever an user uses the URL localhost:80/ , then the response ( res ) should send() “Hello World!” to the client. The result will be the following:

![](https://cdn-images-1.medium.com/max/449/1*L_jjPDM_frALNYZaY-zndA.png)

HURRAY! Your first web application with express.js is running! Now you can create as many routes as you want. You have so much you can do with express.

**Here are some websites where you can learn a lot about express:**

- [https://expressjs.com/](https://expressjs.com/)
- [https://expressjs.com/en/resources/learning.html](https://expressjs.com/en/resources/learning.html)
- [https://developer.mozilla.org/en/docs/Learn/Server-side/Express\_Nodejs](https://developer.mozilla.org/de/docs/Learn/Server-side/Express_Nodejs)
- [https://www.tutorialspoint.com/expressjs/index.htm](https://www.tutorialspoint.com/expressjs/index.htm)

### Conclusion

I hope I could help you a bit setting up your first express.js web application. I am using express for many web application I’ve made so far, and not only for private projects but also for business projects. If you have any questions just comment below!

**Happy coding!**

![](https://cdn-images-1.medium.com/max/1024/1*hYfdBkfKgvtMoDcqk_LjWA.png)<figcaption><a href="https://buttercms.com/static/images/tech_banners/ExpressJS.png">https://buttercms.com/static/images/tech_banners/ExpressJS.png</a></figcaption>