This video is available to students only

How to Create a Minimal Express Node.js Server

Web servers provide functionality for requests that can be made from client applications. Node has a built-in HTTP module to allow for building a server that facilitates the transfer of data in HTTP. With this lesson, we'll look to use the popular Express framework as the replacement of the core HTTP module to create a minimal Node server, with Express routing.

Creating a minimal Node:Express Server

📝 This lesson's quiz can be found - here.
🗒️ Solutions for this lesson's quiz can be found - here.

What we've done so far is use Node to run a simple JavaScript file. Now, we'll look to create a Node server.

A server is software or even hardware that aims to provide functionality for client requests. Large scale applications we use day to day such as Airbnb, Uber, Instagram, and YouTube all have servers that serve data to the respective client apps. Client applications could be running on phones and computers to display this data to users.

Node has a built-in HTTP module that provides the capability to create a server. We'll create a very simple Node server and if you haven't done so before you'll notice how surprisingly little code is needed to make this happen.

package.json

The first thing we'll do is create a package.json file. The package.json file is an important element of the Node ecosystem and is where one can provide metadata about an app, list the packages the app depends on, and create scripts to run, build, or test the app.

We'll create this package.json file at the root of our server project directory.

tinyhouse_v1
  server/
    index.js
    package.json

A package.json file must contain a name and version field. name and version dictate the name of the application package being built and the version of that particular package respectively. We'll name our application package tinyhouse-v1-server and label the version as 0.1.0.

{
  "name": "tinyhouse-v1-server",
  "version": "0.1.0"
}

To help us prepare a Node server, we'll install a third-party library known as Express. Express is an incredibly popular framework for Node designed for building servers and APIs. To install Express, we'll run the npm install command followed by the name of the Express package (express).

server $: npm install express

When complete, npm will fetch Express from its repository and place the express module in a folder called node_modules in our server directory. When we now run our Node application, Node will look for modules that are required in the node_modules folder before looking into parent directories and global installs.

We'll also notice that the package.json file has been updated to include express as a dependency.

server/package.json
{
  "name": "tinyhouse-v1-server",
  "version": "0.1.0",
  "dependencies": {
    "express": "^4.17.1"
  }
}

For anyone else downloading our directory, all they need to do is run npm install to install all the modules listed in the package.json file.

Start a new discussion. All notification go to the author.