How To Create Your First Express.js Web Application

More and more web applications are using ExpressJS. It’s an easy-to-use and powerful web framework for Node.js.

Gilles Heinesch
3 min readApr 11, 2021
Photo by Max Duzij on Unsplash

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:

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.

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:

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:

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 :

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:

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:

Conclusion

There are some books that helped me a lot to better understand the logic and structure of Expressjs. I can only recommend them:

Happy coding!

https://buttercms.com/static/images/tech_banners/ExpressJS.png

--

--