Optimizing your experience
Gradient background

Create the backend with ExpressJS

Page 3 out of 9

ExpressJS claims to be a fast, un-opinionated, minimalist web framework for Node.js. In this chapter, you will create a backend server with a dummy API that your frontend will connect to, create a test endpoint where your client says ping and the server says pong and then you will create an endpoint to rate reviews albeit it be in-memory for now.

Clarice Bouwer

Software Engineering Team Lead and Director of Cloudsure

Thursday, 3 November 2022 · Estimated 3 minute read
Modified on Sunday, 13 November 2022

Objectives

  1. Learn some ExpressJS.
  2. Create a backend server with a dummy API that your frontend will connect to.
  3. Communicate between the server and client with a ping pong endpoint.
  4. Create an API endpoint to rate reviews in-memory.
  5. Test the endpoint using a tool like Postman.

Get Started

Node.js

Node.js is an open-source and cross platform runtime environment for executing JavaScript code.

ExpressJS

ExpressJS claims to be a fast, un-opinionated, minimalist web framework for Node.js. ExpressJS is also the most popular Node.js web server framework and is the basis of thousands of sites. What you’ll learn in the video below:

  • Basic routing
  • Sending data
  • Rendering HTML and static files
  • Routers and advanced routing
  • Middleware
  • Parsing form/JSON data and query params

Restructure

Adding a server requires the addition of a new npm project with its own package.json file. To avoid the complexities of setting up a monorepo, you are going to move the web application into a directory of its own.

>./
Copy
mkdir -p web; mv * $_

Update .gitignore to exclude node_modules/ (note the forward-slash is at the end). This directory will now be excluded from anywhere within your project instead of just the root directory.

Create the ExpressJS server

Create a server directory in the root of the project and initialize it as an npm project. Install the express npm package and then create the server.js entry point file.

>./
Copy
mkdir server
cd server
npm init -y
npm install express
mkdir src; touch src/server.js

Enable modern JS

Configure your server to use modern JavaScript (such as import statements) by editing the package.json and specifying type as module.

./server/package.json
Copy
{
  "type": "module",
}

Run the server

Add the following script to your package.json file under scripts.

./server/package.json
Copy
{
  "start": "node src/server.js"
}

And run the server using

>./server
Copy
npm start

🎾 Ping pong

Create a new Express server listening on port 3001. In this basic endpoint example, /ping should respond with "pong!". In other words, open your browser and navigate to http://localhost:3001/ping to see the text pong! on the screen.

./server/src/server.js
Copy
import express from 'express';

const app = express();

app.get('/ping', (req, res) => {
  res.send('pong!');
});

app.listen('3001', () => {
  console.log("Listening on http://localhost:3001")
})

Automatic updating

Let's update the server to use nodemon so that we can avoid manually restarting the server when changes are made.

>./server
Copy
npm install nodemon --save-dev
./server/package.json
Copy
{
  "start": "nodemon src/server.js"
}

Develop the API

Create a temporary in-memory ratings array with 3 objects in it. The endpoint to hit will be http://localhost:3001/api/review/:id/rate/:rating where id and rating are variables that will correlate to attributes in the objects within the array.

./server/src/server.js
Copy
import express from 'express';

// This in-memory structure be replaced by real database later on.
const inMemRatings = [
  { id: 1, rating: 0 },
  { id: 2, rating: 0 },
  { id: 3, rating: 0 },
];

const app = express();

// A built-in middleware function that parses incoming JSON requests and puts the parsed data in req.body.
app.use(express.json());

// A PUT verb on the following endpoint to update the rating and respond to the client accordingly.
app.put('/api/review/:id/rate/:rating', (req, res) => {
  const { id, rating } = req.params;
  const item = inMemRatings.find((r) => r.id === parseInt(id, 10));
  if (item) {
    item.rating += parseFloat(rating);
    res.send(item);
  } else {
    res.send('Not Found');
  }
});

Test the request in an API tester like Postman and remember to set the method to PUT.

>./
Copy
curl --location --request PUT 'http://localhost:3001/api/review/1/rate/1'

Next steps

Time to get serious and implement a real database. I've chosen to explore MongoDB for this course but you could essentially connect to any database you would like to.

References