REST
Neil Haddley • February 6, 2021
Creating a REST API Service using Node and Express
HTTP Methods/Verbs
Hypertext Transfer Protocol (HTTP) defines a set of request methods that a client application will use to ask an HTTP server to do something. HTTP request methods are often called verbs. HTTP verbs include:
Get A client application will use a Get method to retrieve something from an HTTP server.
Post A client application will use a Post method to send something to an HTTP server.
Put A client application will use a Put method to request that something managed by an HTTP server is updated.Delete A client application will use the Delete method to request that something managed by an HTTP server is thrown away.
API Routes
Routing determines how an HTTP server will respond to a client request. The client application will specify a path and an HTTP verb.
Route parameters are passed as part of the URL provided to the HTTP server.
https://localhost/books
request details of all books
https://localhost/books/1788395549
request details of the book with ISBN Number 1788395549
Route handlers and Response methods
Route handlers are functions that execute on the HTTP server when a request is received from the client application.
Response methods are used to send something back to the client application. Response methods include:
send() Sends a response to the client.
sendFile() Sends a file to the client.
redirect() Redirects the request.
json() Sends a JavaScript object to the client.
status() Sets the HTTP status code
Using Node.js and Express
npm can be used to initialise a REST API project.
$ mkdir books
$ cd books
$ npm init
$ npm i express
The package.json file
The package.json file generated by npm should be updated to:
The index.js file
The index.js file generated by npm should be updated to:
The Dockerfile
Visual Studio Code is able to add a Dockerfile to a Node.js project automagically.
Open the Command Palette (⇧⌘P) and use the "Docker: Add Docker Files to Workspace..." command:
FROM node:12.18-alpine
ENV NODE_ENV=production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json", "npm-shrinkwrap.json", "./"]
RUN npm install --production --silent && mv node_modules ../
COPY . .
EXPOSE 5000
CMD ["npm", "start"]

REST API Service

/books route

/books/id route with a valid id

/books/id with an invalid id
Example route handlers
JAVASCRIPT
1app.get('/books', (request, response) => { 2 response.json(books); 3}); 4 5app.get('/books/:id', (request, response) => { 6 const isbnId = Number(request.params.id); 7 const found = books.find((book) => book.id === isbnId); 8 9 if (!found) { 10 response.status(404).send('Book Not Found'); 11 } else { 12 response.json(found); 13 } 14});
package.json
JAVASCRIPT
1{ 2 "name": "books", 3 "version": "1.0.0", 4 "description": "", 5 "main": "index.js", 6 "scripts": { 7 "start": "node index.js" 8 }, 9 "author": "", 10 "license": "ISC", 11 "dependencies": { 12 "express": "^4.17.1" 13 } 14}
index.js
JAVASCRIPT
1const express = require('express'); 2const app = express(); 3let books = [ 4 { 5 "id": 1788395549, 6 "title": "Learning Node.js Development", 7 }, 8 { 9 "id": 1788620216, 10 "title": "Hands-On Microservices with Node.js", 11 } 12]; 13 14app.get('/books', (request, response) => { 15 response.json(books); 16}); 17 18app.get('/books/:id', (request, response) => { 19 const isbnId = Number(request.params.id); 20 const found = books.find((book) => book.id === isbnId); 21 22 if (!found) { 23 response.status(404).send('Book Not Found'); 24 } else { 25 response.json(found); 26 } 27}); 28 29 30app.listen(5000, () => console.log('server running'));