Getting started with Node.js/Express: Creating a simple web server.

Prerequisites:

This tutorial assumes that you have basic knowledge of:

  • JavaScript fundamentals
  • The difference between the client-side and server-side of web applications
  • HTTP requests and definition of web servers.
  • The Command Line Interface.

Introduction

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It is a JavaScript framework used to run JavaScript code outside the browser environment, and as such is commonly used to power the backend (server side) of web applications. Express is a Node.js web framework which builds on top of Node's features to provide easy to use functionality, running between the server created by Node.js and the frontend pages of the web application.

In this tutorial, we'll be creating our first Node.js web server with Express.

Setting Up Your Development Environment

First, ensure you have Node installed on your machine. To check, open your command line tool and type node -v. If installed, the version of Node.js you have installed on your machine will be displayed. If you do not have it installed, you could check this article for instructions on installing Node.js.

Now that you have that set up, create a directory for the project using the command mk-dir project-name (where project-name is the name of your directory) and navigate to the directory with the command cd project-name. To initialize the project and create a package.json file, we run the command npm init. A package.json file contains basic information about your project such as the name, author, version, description and dependencies.

npm (Node Package Manager) is a command-line tool used to share Node.js packages/modules. A dependency is a Node.js package used in building your project.

To install a package in your project, navigate to your desired directory and run npm install package-name. to install a package globally, run npm install package-name --global (you could use the shortcut -g instead of --global). To install Express in this project's directory, we'll run the command npm install express --save. Using the --save (shortcut -s) flag adds the package to the list of dependencies in your package.json file.

At this point, we are done setting up our development environment and can now proceed to the project proper.

Writing the Code for the Web Server

In your text editor, create an index.js (or whatever name you choose) file in your directory.

First, we'll import the express module we installed:

const express = require('express')

Then we instantiate express by calling its app() method (express()) and assigning this to a variable:

const app = express()

Next, we tell app to listen for GET requests to our server on the specified path (in this case /) and send a specific response, using the get() method:

app.get('/', (req, res) =>
res.send("I created a Node.js Web Server!"))

req and res here represent the Request and Response objects, where Request is the HTTP Request and Response is the HTTP response object sent to the client in return.

Express has similar methods for every HTTP verb: post() for POST, put() for PUT etc.

Finally, we start our web server by telling the app to listen on a specific port (3000 in this case) and run our callback function when the server starts up:

app.listen(3000,  () =>
console.log"Success!")

Starting the Server

To run the code that we have just written and start up the server, simply run node index.js in your command line interface, then open localhost:3000 in your browser. You should see your response displayed.

Conclusion:

Now that you've gotten started with Node.js/Express by creating a web server, you can learn more by following this learning path and strengthen your skills by building simple projects such as these. Best of luck!