Share
Explore

Building Web Sites with Microservices

Background and theory of microservices:
Considering the course learning outcome:
Develop web applications that follow a microservice architecture and leverage both internal and external APIs
Develop a simple REST API using express.js that supports the basic HTTP Verbs: ​GET, POST, PUT, and DELETE requests
3.3 Use industry standard API testing tool(s) to test the functionality of various API endpoints:
PostMan
Lab to make our own Postman : using Node.js

Lab Part 2: Building the Backend Data Store with MONGO DB:

Learning Outcomes:

How to Develop web applications that follow a microservice architecture and leverage both internal and external APIs:

Lab code examples using Node.js NPMJS packages

Lab Learning Workbook: Microservices Architecture Web Development

Part 1: Introduction

In this lab, we will cover the fundamentals of building a microservice-based architecture using Express.js, a popular Node.js framework. We will build a simple REST API, use it internally, and learn to leverage external APIs.
Activity A: A Simple Example of Nodejs Application Development
Step 1: npm init Makes package.json
Let’s setup a simple Express JS Server:
Step 1: install from
Express package

Part 2: Setting Up

Install Node.js and npm (Node Package Manager) if you have not done so:
Once Node.js is installed, npm will also be installed along with it.
Now, create a new directory for your project:

mkdir microservice-labcd microservice-lab
Initialize your Node.js application:
bashCopy codenpm init -y
Install Express:
bashCopy codenpm install express

Part 3: Building a Simple REST API with Express.js

We will start by creating a simple book management system.
Create a new file called server.js and include the following code:
const express = require('express');const app = express();app.use(express.json());
let books = [];
app.get('/books', (req, res) => { res.json(books);});
app.post('/books', (req, res) => { const book = req.body; books.push(book); res.status(201).send();});
app.put('/books/:id', (req, res) => { const id = req.params.id; const book = req.body;
let index = books.findIndex((book) => book.id === id); if (index !== -1) { books[index] = book; res.send(); } else { res.status(404).send(); }});
app.delete('/books/:id', (req, res) => { const id = req.params.id;
let index = books.findIndex((book) => book.id === id); if (index !== -1) { books.splice(index, 1); res.send(); } else { res.status(404).send(); }});
app.listen(3000, () => console.log('Server running on port 3000'));
This server will start a server at localhost on port 3000. We have routes to GET, POST, PUT, and DELETE books.Nodemon is a utility that will monitor for any changes in your source code and automatically restart your server. Perfect for development. Here's how you can get it running with your server.
Nodemon will watch the files in the directory in which nodemon was started, and if any files change,

Nodemon is a utility that will monitor for any changes in your source code and automatically restart your server. Perfect for development. Here's how you can get it running with your server.

First, you'll need to install nodemon. If you haven't installed it yet, you can do so by using npm (node package manager):
bashCopy codenpm install -g nodemon
This command installs nodemon globally so you can use it in any Node.js project.
Then, instead of starting your server with node server1.js, you can start it with nodemon server1.js.
bashCopy codenodemon server1.js
Nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
For using nodemon as a development dependency and adding a start script in your package.json:
Install nodemon as a development dependency:
bashCopy codenpm install --save-dev nodemon
Open package.json in your root directory and add a start script:
jsonCopy code"scripts": { "start": "nodemon server1.js"}
Now you can start your server with the following npm command:
bashCopy codenpm start
Now, every time you save your file, nodemon will automatically restart your server, thus you won't need to stop and restart it manually every time you make a change.

Write an HTML page with forms to interact with and allow user interaction with each of the routes to add and read the details of book objects

A basic HTML form that can interact with our Express.js server using JavaScript's built-in fetch function.
In this example, we'll only cover creating a form to add a book (POST /books) and display all books (GET /books).
<!DOCTYPE html><html><head> <title>Book Management System</title> <script> // Function to add a new book async function addBook() { const title = document.getElementById('title').value; const author = document.getElementById('author').value; const id = document.getElementById('id').value;
const response = await fetch('http://localhost:3001/books', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id, title, author }) });
if (response.status === 201) { document.getElementById('message').textContent = 'Book added successfully!'; loadBooks(); } else { document.getElementById('message').textContent = 'Error occurred while adding the book.'; } }
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.