How to create your first Node Server using Express and JavaScript.

YASH RAJ
3 min readJun 29, 2020

--

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

Required Installation:

  1. Node.JS
  2. VS Code(You can use any IDE or editor)
  3. Express.js
  4. Nodemon

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

$ cd Desktopmkdir 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": "yashraj",
"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": "yashraj",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}

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

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/.

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": "yashraj",
"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

There you have it. Now you have created your first express server and two endpoints.

--

--

YASH RAJ
0 Followers

I write about tech, business strategies, insights on my personal blog. https://intotheinsights.com/articles