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.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

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.

This lesson preview is part of the The newline Guide to Building Your First GraphQL Server with Node and TypeScript course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

Unlock This Course

Get unlimited access to The newline Guide to Building Your First GraphQL Server with Node and TypeScript, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course The newline Guide to Building Your First GraphQL Server with Node and TypeScript