Creating a Express Server using Node Part 1: Hello World

boyloe

Bryan Oyloe

Posted on November 9, 2020

Creating a Express Server using Node Part 1: Hello World

So you want to build a website but you don't know where to begin when it comes building a backend for your data? Node and Express provide a flexible set of tools to build up your backend. Node is a runtime that allows programmers to write JavaScript outside of the browser. This can be very powerful when it comes to building data servers. In this first post I will be taking you through the "Hello World" setup for your first Node backend.

First Steps

To begin, you will have to install node to your system. You can run this line of code to get nvm installed on your system.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Enter fullscreen mode Exit fullscreen mode

If you type

command nvm -v
Enter fullscreen mode Exit fullscreen mode

Your terminal should print "nvm" to the screen. Nvm will now allow us to install Node and use npm(Node Project Manager) directly from the terminal. Now we can install Node with the following commands:

nvm install node
nvm use node
nvm alias default node
Enter fullscreen mode Exit fullscreen mode

You are also going to need a text editor. I will be using VSCode but there are many other fine editors.
Now that we have node installed, open your terminal and create a directory for your new project. I have called the project "Part1-HelloWorld" on my machine. After creating, make sure you are in your new folder and type npm init. The npm init command initializes node for your project, and will ask a series of questions on how you want to format your node packages for this project. If you want to accept all defaults you can use the -y flag like so. 
 Your terminal should look like this:

Part1-HelloWorld npm init -y
Wrote to /Users/bryanoyloe/Flatiron/Mod5/BlogPosts/NodeExpress/Part1-HelloWorld/package.json:

{
  "name": "Part1-HelloWorld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}


➜  Part1-HelloWorld
Enter fullscreen mode Exit fullscreen mode

The purpose of the package.json is to store the dependencies, scripts, and metadata for your project. The packages we are going to install will be placed in a node_modules file which contains the actual code these packages use to run.

Installing Packages

The next step is to install the packages we are going to need to get up and running. Since we are just going to be doing a simple "Hello World", our package needs are minimal. In your terminal type

npm install express 
Enter fullscreen mode Exit fullscreen mode

then

touch index.js
Enter fullscreen mode Exit fullscreen mode

The index.js file will be our main file for spinning up our server. Open your project folder up in your text editor and navigate to your index.js. In the index.js file type the following:

const express = require('express')
const app = express()

app.listen(4000)
Enter fullscreen mode Exit fullscreen mode

Here we are requiring the express package from our node modules and creating an instance of express, assigning it to a const app. App is an arbitrary name and we could call it anything we want, however using the constant app follows convention. Finally we are telling our app instance to listen for HTTP requests on port 4000. This is going to allow us to make requests to our server, and get information from our server. Now we are ready for our "Hello World" moment. We need to write some code that tells our server how to respond to requests that we make. First, we will be creating an action that responds to a GET request from a web browser. Insert this code into your index.js file.

app.get('/', (request, response) => {
    response.send("Hello World")
})
Enter fullscreen mode Exit fullscreen mode

This code is handling a GET request to the '/' or 'Home' path. The first argument is the path that we want the action to handle, and the second is a callback function that takes in the request and response as arguments. Our callback function is simply telling our action to send the string "Hello World" with the response. In your terminal type

node index.js
Enter fullscreen mode Exit fullscreen mode

and open up your internet browser. Navigate to http://localhost:4000 and you should see the "Hello World" response from your server!

That's it for the first installment of this series on Node. In the next part I will be discussing how to begin talking to a database so that you can persist information and pull it from the database as needed. Stay Tuned.

πŸ’– πŸ’ͺ πŸ™… 🚩
boyloe
Bryan Oyloe

Posted on November 9, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related