Community Articles Tutorials
github-logo youtube-logo

Node.JS + Express Server Setup

A Guide to Setting Up an Express Server

post-image

0. PREREQUISITES

1. CREATE A FOLDER TO STORE YOUR PROJECT

2. OPEN THE PROJECT FOLDER IN VSCODE

3. INITIALIZE NODE PROJECT/ADD PACKAGE.JSON FILE TO PROJECT

From terminal, run:

> npm init -y

***If terminal is not visible, use shortcut ctrl+`

4. INSTALL EXPRESS WEB FRAMEWORK

From terminal, run:

> npm i express

***If terminal is not visible, use shortcut ctrl+`

After installing Express, it will appear as a dependency in package.json file

5. INSTALL NODEMON AS A DEV DEPENDENCY

From terminal, run:

> npm i -D nodemon

***If terminal is not visible, use shortcut ctrl+`

After installing nodemon, it will appear as a dev dependency in package.json file

6. ADD NODEMON SCRIPT TO PACKAGE.JSON

Add name/value pair within "scripts"

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

7. EXPRESS SERVER SETUP

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

app.listen(PORT, () => console.log(`Server listening on port ${PORT}`))
  • LINE 1: Import express API
  • LINE 2: Initialize const variable for port express will communicate on
  • LINE 3: Initialize express object
  • LINE 4: blank
  • LINE 5: Use listen funciton on express object to start express server. Function takes a port number and a function. We provide it the int value stored in the const variable PORT and the function will log the provided string to the console

8. START EXPRESS SERVER

From terminal, run:

> npm run dev

***If terminal is not visible, use shortcut ctrl+`

Terminal will output the following:

> dev
> nodemon server.js

[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server listening on port 8080