Express (Mock API Using Node)

I needed a quick and fast Restful API mock that will run in a container. Normally we use Wire Mocks which is a .Net libruary but Node is much faster right? Ohh shots fired! 🤣

Create And Test App

  1. Use NPM to scaffold some template files
1
2
3
4
5
6
mkdir mymock
cd mymock

npm init ~ populate the things it asks or just enter like a mule into Mexico
npm init --yes ~ alternatively use the --yes flag to use defaults, kindof like a blind mule? :D
npm install --save-dev express
  1. Create index.js, you may have called this something related to your mocked service and pop in your mocked routes. 5007 be any avalible port, check what ports are already mocked in your repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import express from "express";

const app = express();
const port = 5007;

app.get("/foos/:fooid*", (req, res) => {
res.json({
params_fooid: req.params.fooid,
params_barid: req.query.barid,
});
});

app.listen(port, () => {
console.log(`Mock listening on port ${port}`)
})

You can also access environment variables should they exist

1
const myVar = process.env.THIS_IS_MY_VAR
  1. Edit package.json and add
1
2
3
4
"type": "module",

"scripts": {
"start": "node .",
  1. Run the script with npm start from your console. You can then test it from your browser at http://localhost:5007/foos/1

You should get

1
{"params_fooid":"1","params_barid":"2"}

You can then refactor the app to mock any routes and results, for the above we are mocking /foos/:fooId so /foos/1 which responds with some json and express will ensure status 200 OK (its like magic)

  1. Stop the app with CRTL C which will free up port 5007.

  2. You will probably push this to git, its common if part of a mono-repo to now have a .gitignore for mymock as it is an application.

1
2
# Dependency directories
node_modules/

Docker

Do you even code if you dont package up your things into a nice container :D

  1. Create the .dockerignore file and add
1
2
node_modules
build

.dockerignore is a configuration file that describes file(s) & directory(s) that you want to exclude when building a Docker image.

  1. Create the Dockerfile. node:slim is a lightweight node image and npm ci will clean install the project. The healthcheck fails then exit with code 1. Any code other than 0 means there was some sadness.
1
2
3
4
5
6
7
8
9
10
11
12
13
FROM node:slim
WORKDIR /usr/src/app

COPY package*.json ./
RUN npm ci

COPY . .
HEALTHCHECK --interval=10m --timeout=5s \
CMD curl -f http://localhost/ || exit 1

EXPOSE 5007

CMD [ "npm", "start" ]
  1. Create an image and test it the same as above at http://localhost:5007/foos/1.

Some notes

  • -t tags the new image
  • -it means create an interactive bash shell in the container and connect to it
  • -rm removes the container when it exits.
1
2
docker build -t mymockimage:1 .                                   ~ . means build in the current dir context
docker run -it --rm -p 5007:5007 --name mymock mymockimage:1
  1. Stop the app with CRTL C which will free up port 5007.

  2. Cleanup the image

1
docker image rm mymockimage:1

References