Italktech.io

How to create your first Node Server using Node and Express

August 5, 2019 • ☕️ 3 min read

express.js

The main idea here is to explore more about Express.

Express is a popular JS library that allows you to build a JavaScript server using Node.js.

For this tutorial, we will need to make sure we have a couple of things installed.

Node.JS ( I recommend the LTS version ) A text editor or IDE ( I recommend VS Code) Express.js Nodemon

First step

If you have Node.js installed, you can start here on the first step. We will start creating a directory folder for our app.

I will be creating mine on the desktop and call it express-tutorial On a mac, you can follow my steps opening the terminal.

 $ cd Desktop 
mkdir express-tutorial 

This command will go to the desktop and create the express-tutorial folder.

Then all you need to do is navigate to your folder, and you can do it with the following command.

 $ cd express-tutorial 

Now all you need to do is to start a new npm project, and we can do it easily using npm( Node package manager) with the following command.

$ npm init -y

It will create a default package.json with the following shape:

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

Now we will install the express using the following command:

$ npm install express

The command will add the Express to your dependencies, and now if you recheck your package.json you can see Express under dependencies that will be looking like

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

The nex thing is to create a new file in the root of your project, let’s name it server.js

directory

The first thing we need to do in our server js is to import Express and initiate it.

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

Now we will define the port number and config express to listen for it.

const PORT = 3000
app.listen(PORT)

The next step is to define our first route endpoint. Let’s create the first version of our route.

// End points router
app.get('/', function (req, res) {
 res.send('Hello World') 
})

Now if you run on your terminal inside the project folder node server.js the nodejs will run Express and serve it on the port 3000, and we can test it hitting the http://localhost:3000/.

hello-world

Now we have an express server running, but it’s not the best developer experience to use it, because we need to stop the server and start again every time we want to see some changes. To fix it, we will use Nodemon. Let’s install it using:

$ npm install  --save-dev Nodemon

If you recheck the package.json, you will notice that nodemon now is under devDependencies. And the reason been is because nodemon is a tool that will improve the developer experience, but it’s not a project dependency.

{
 "name": "express-tutorial",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
 "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": [],
 "author": "",
 "license": "ISC",
 "dependencies": {
 "express": "^4.17.1"
 },
 "devDependencies": {
 "nodemon": "^1.19.1"
 }
}

next step is to add a script to start the server for us, let’s do it. Under scripts in your package.js add the following line:

"scripts": {
 "test": "echo \"Error: no test specified\" && exit 1",
 "start":"nodemon server.js"
 },

now on your terminal, you can run it using the following:

$ npm run start

nodemon

This command will trigger the start script, and it will run nodemon and start your server.js

The beauty of nodemon is that it does auto-reload for you and update your server every time that you save it.

So now let’s start the server and add a new endpoint to test it. After starting your server lets create a new router called names

I will define new array names and return it on our new endpoint ‘/names’.

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

const names = ["Jean", "John", "Frank"]

// End points router
app.get('/', function (req, res) {
 res.send('Hello World') 
})


app.get('/names', function (req, res) {
 res.send(names) 
})
 
// Port that the server is running
app.listen(PORT)

Now let’s navigate to http://localhost:3000/names.

names

There you have it. Now you have created your first express server and two endpoints, and you have set up for the first time your nodemon to improve your development experience.

You also can follow this tutorial on Youtube:

Please let me know if you have any questions!

Regards,

Jean Rauwers