What is Postman?

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.

API Tools

A comprehensive set of tools that help accelerate the API Lifecycle - from design, testing, documentation, and mocking to discovery. Start explore and install here

In short, Postman offer alternative solution of frontend like website to send all type of http request to server without the hassle of making a client.

The API request object including

It's time to build a backend. We'll do so by using a NPM package named Express.

Web Applications

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

APIs

With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy.

Performance

Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love.

Every backend will do the same thing.

Allow us to Create, Read, Update, Delete(C.R.U.D.) a resource/entity/foo in our database through a URL using REST.

Route Name

URL

HTTP Verb

Description

Index

/foos

GET

Get a list of foos

Create

/foos

POST

Create a new foo

Show

/foos/:id

GET

Get a detailed foo

Edit

/foos/:id

PUT/PATCH

Update a property/attribute/params of a foo

Destroy

/foos/:id

DELETE

Delete/Destroy/Remove an individual foo from the list of foos

The URLs can also "listen" for query parameters and produce dynamic results/output/data.

Query String Params

We must also handle the following URLs.

Route Name

URL

HTTP Verb

Description

Index

/foos

GET

Get some foos. Defaulting to pg=1 & limit=20

Search for spam

/foos?q=spam

GET

Find a foo where it's title/name/description contains spam

English Foos

/foos?language=en

GET

Get some foos where the language property/attribute/params of the foo is English

English Foos sorted

/foos?language=en&sort=asc

GET

Get some foos where the language property/attribute/params of the foo is English & sorted in asc order

Create some mock api enpoints with express and test with postman

Requirements

  1. As a Postman APP I can make a GET request to http://localhost:5000/news and receive back a message Successfully get all news
  2. As a Postman APP I can make a GET request to http://localhost:5000/news?page=2 and receive an message, the Successfully get page 2 news
  3. As a Postman APP I can make a GET request to http://localhost:5000/news?page=3 and receive an message, the Successfully get page 3 news
  4. As a Postman APP I can make a GET request to http://localhost:5000/news?page=n and receive an message, the Successfully get page n news
  5. As a Postman APP I can make a GET request to http://localhost:5000/news?q=Facebook and receive back a message Successfully get all news related to query Facebook.
  6. As a Postman APP I can make a GET request to http://localhost:5000/news?q=anyString and receive back a message Successfully get all news related to query anySring.
  7. As a Postman APP I can make a GET request to http://localhost:5000/news?title=Mobile and receive back a message Successfully get all news that have tittle Mobile or mobile.
  8. As a Postman APP I can make a GET request to http://localhost:5000/news?title=dynamicValue and receive back a message Successfully get all news that have tittle according to dynamicValue.
  9. As a Postman APP I can make a GET request to http://localhost:5000/news?city=New York and receive back a message located in New York.
  10. As a Postman APP I can make a GET request to http://localhost:5000/news?category=dynamicValue. and receive back a message Successfully get all news that have category according to dynamicValue.
  11. As a Postman APP I can make a GET request to http://localhost:5000/news?category=dynamicValue&city=dynamicValue&title=dynamicValue. and receive back a message Successfully get all news that have a string of all queries and theirs dynamicValue.
  12. As a Postman APP I can make a GET request to http://localhost:5000/news/:id and receive a message Successfully get detail of 1 single new with the id is dynamicValue
  13. As a Postman APP I can make a POST request to http://localhost:5000/news with a request body contain title,category,city and receive a message Successfully create a news about title in city and related to category . Example below. Example request body
{
  "title": "Morning HCM",
  "category": ["daily", "general", "covid19"],
  "city": "HCM"
}
Successfully create a news about Morning HCM in HCM and related to category of daily, general and covid
  1. As a Postman APP I can make a PUT request to http://localhost:5000/news/:id with a request body contain title,category,city and receive a message Successfully find the news with id , and update the content with title in city and related to category . Example below.
http://localhost:5000/news/1234
{
  "title": "Afternoon HCM",
  "category": ["daily", "general", "covid19"],
  "city": "HCM"
}
Successfully create a news about Morning HCM in HCM and related to category of daily, general and covid
  1. As a Postman APP I can make a DELETE request to http://localhost:5000/news/:id and receive a message Successfully find the news with id , and delete
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const cors = require("cors");
const indexRouter = require("./routes/index");

const app = express();
app.use(cors());
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/news", indexRouter);

Cors

CORS is a node.js package for providing a Connect / Express middleware that can be used to enable CORS with various options It help us disable CORS error. Readmore here

Morgan Logger

Logger is a technique in programming to keepin log of how an program running. It contain many errors such as process, success and error.

Morgan is an HTTP request logger middleware for node.js Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names), a string of a format string, or a function that will produce a log entry.

The format function will be called with three arguments tokens, req, and res, where tokens is an object with all defined tokens, req is the HTTP request and res is the HTTP response. The function is expected to return a string that will be the log line, or undefined / null to skip logging. Readmore here

Path

In Node.js, the path module is an inbuilt module which deals with path and directories. Every Operating System has its own way of managing paths and the operations related to it.

For examples different OS will have different syntax to navigate in the file system. Path module help Node to adapt on the native OS syntax

Readmore here

Cookie parser

This module help expose the cookies send along with client request. Which may use to help authorization , cache , ... Parse Cookie header and populate req.cookies with an object keyed by the cookie names. Optionally you may enable signed cookie support by passing a secret string, which assigns req.secret so it may be used by other middleware. Readmore here

Scalable

In practices an api server may have multiple endpoints. We tend to group endpoint with the same nature together. For examples, if we have the same amount of routes for each feature Create, Read, Update, Delete like the exercise. Then apply that for:

Since each endpoints listed above serving 1 type of data. It is recommended that we group them in separated file

File Structure and Index routes

Commonly ruotes folder will contain all endpoints and index will connected to each one and act as a mini router in our backend.

|- ruotes/
  |- index.js
  |- users.api.js
  |- authors.api.js
  |- movies.api.js
  |- news.api.js

Since we are working with multiple files in our codebase, it is very important that we understand how each line and file relate to eachother. A request from client will always go through its specific route to a result reponsding (success or error)

Flowchart : Execution

Here is A flow chart on how express file work that may help us to visuallize the process of the files

In this exercise you have the chance to write app.js for the first time and maybe the last time since later on express generator would automated the file. However, having understanding on how and why each line of app.js is written will definitely solidify your backend skills.

Example

//Load the express module installed to constant express
const express = require("express");
...
//Execute and use express method translating data to json() format as App level middlewares.
app.use(express.json());

Express next()

So we have explore req res the only thing left in the Express Router callback arguments is next()

In short, next() is a method, when called, start executing/calling/running the next middlewares in the line.

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

app.get("/same", function (req, res, next) {
  console.log("before request handler");
  next();
});

app.get("/same", function (req, res, next) {
  console.log("handling request");
  res.sendStatus(200);
  next();
});

app.get("/same", function (req, res, next) {
  console.log("after request handler");
  next();
});

app.listen(3000, function () {
  console.log("Example app listening on port 3000!");
});
before request handler
handling request
after request handler

Try comment out the call to next() in the middle handler like this:

app.get("/user/:id", function (req, res, next) {
  console.log("handling request");
  res.sendStatus(200);
  //next();
});
before request handler
handling request

Notice that the last handler (the one that prints after request handler) does not run. That's because you are no longer telling express to run the next handler.

So it doesn't really matter if your "main" handler (the one that returns 200) was successful or not, if you want the rest of the middlewares to run, you have to call next().

Remember that node is async so it can't know when the first handler's callback has finished. You have to tell it by calling next().

Again here is A flow chart on how express file work that may help us to visuallize the process of the files

Error handling

The final puzzle in our Express code is Error handling middleware, which help us to handle in the event of error happen in our server code

app.use((err, req, res, next) => {
  console.log("ERROR", err.message);
  return res.send(err.message);
});

Notice the err argument, is carrying the Error object passed by method before it. We will once again using this flow chart on how express file work to undestand the logic of Error handling

The magic control Try-catch(err)-next(err)

We doing a task with our server, no one could make sure that it will always success. So in the case of failure , we must be able to identify and handle it. Recall try-catch in previous weeks, wrapping tasks inside of try-catch help us control when "thing go wrong, do this instead of crashing my precious server".

try {
  //dosomethiing that may be success or fail
} catch (err) {
  //dosomething when it fail and have err object
  next(err); // calling next-in-line-error-handling-middleware
}

Which is this middlewares in app.js

app.use("/api",ABCXYZ) // see how this middlewares and it handler ABCXYZ run in prior

...
app.use((err, req, res, next) => {
  console.log("ERROR", err.message);
  return res.send(err.message);
});

Throw

So sometime we want to make an error just for fun we could use

throw new Error("error message");

and inside a try-catch your new error will be catch by the error catch handler.

try {
  //dosomethiing that may be success or fail
  //but too lazy to do anything
  throw new Error("too lazy");
} catch (err) {
  //dosomething when it fail and have err object
  console.log(err.message); // too lazy
  next(err); // calling next-in-line-error-handling-middleware
}

In express app structure, we treat Error like bomb (in the game Pass the bomb) Where all bombs must be handle but only a specialized bomb squad should handle the bomb. So we pass it until reaching the Error Handler app method

In this exercise you will create :

Hey there, travelers! if you want to learn everything about Express and RestfulAPI that has not been covered (lots of it), check out the links below to have better understanding of the concepts.