Skip to content

Getting Started with Express.js

Last updated on November 21, 2022

In this post, I would like to write getting started steps for the Express.js framework.

What is Express? Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It’s an easy-to-use node.js framework for web applications.

This post is part of a series called – Getting Started with Express.js
How to Write Middleware for Express.js Apps
Getting Started with Express.js

Installing Express.js

Let’s begin by installing it(assuming we already have Node.js installed). First, create a directory to hold your application, if you have not already done create a directory. I named my directory “myapp”.

$ mkdir myapp
$ cd myapp

Now open your terminal, change to it, and run npm init. init command will generate a package.json file in the current directory in our case in myapp.

npm init

After running ‘npm init’ you’ll be prompted with several questions. You can fill them out or hit enter to get through it. Now our package.json is ready now install Express in the “myapp” directory and save the dependencies and update the package.js file with the dependencies list:

 npm install express --save 

To install Express temporarily(don’t save the dependencies list) omit the “–save” option.

To verify our Express installation just create a file named app.js and add the following code:

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

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

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

We have created a new server using ‘app.listen’ and setting the HTTP GET on the app to send a ‘Hello World’ message. Other than root URL (/) and For every other path, it will respond with a 404 Not Found.

Now we need to start the server.

node app.js  

Awesome! Open a browser and head for http://localhost:3000 where you should see the ‘Hello World’ message.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments