Premium lesson

Writing Your First Web Server in Express.js

Express.js·3 min read·Jan 1, 2025

A web server is a software application responsible for delivering web content, such as web pages or data, to clients over the internet or a local network.

It listens for incoming HTTP requests sent by clients, such as web browsers or web services, interprets those requests, and sends the requested content back to the clients in the form of an HTTP response.

It can serve static content, like pre-written HTML files, or dynamic content, which is generated in real-time and often involves additional components like databases, enabling the creation of personalized web experiences.

Setting up a new project

Let's start by creating a new directory named web-server:

$ mkdir web-server$ cd web-server

Generate the package.json file using the npm init command:

$ npm init

And install the Express framework available on the npm registry as the express package:

$ npm install express

Initializing the web server

Let's create a new file named app.js within the web-server directory:

$ touch app.js

Within this file, let's first import the express package:

icon light bulb key

Unlock the Build RESTful APIs in Node.js module

Learn how to build real database-backed and framework-powered RESTful APIs with MySQL, Sequelize, and Express.

You get immediate access to:

  • 37 focused lessons across MySQL, Sequelize, backend, and Express
  • 4 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module