5 Steps to Build a Node.js Mock Server and API With Random Data

As a front-end developer, you often need data from an API. But sometimes the back end hasn’t been set up by your team. In order for you to continue and mock your data, it’s a good idea to not store your data structure on memory.

It’s a good idea to start as soon as possible in talking with an external API where your data comes from. In this tutorial, I want to go through a minimal setup for mocking your data. After this, you should be able to extend it with your own data as needed.

1. Dependencies

In order to work with Node, you need to have it installed on your machine. For Mac users, I highly recommend installing it with nvm because it’ll make it easier to update Node.js in the future. There’s also a version for Windows.

Create a new folder for this project. Run npm init -y to initialize the folder with Node, and it’ll automatically create a package.json for you.

In order to create the mock server, we need two npm dependencies: [json-server](https://www.npmjs.com/package/json-server) and [casual](https://www.npmjs.com/package/casual). We run npm install json-server casual — save-dev in our project.

2. Base for the Mock Server

Create an index.js, and paste this code into it.

We include the json-server in order to use it. Then we create a server instance in the const server. With the middlewares, we can set a few options — like the path to static files, CORS, and a few more. But here we just use it without any specific options.

The port is very important. If you want this to run on a server, it’ll first search if there’s any default port set for a Node server; otherwise, it’ll pick port 3000.

We include the bodyParse and middleware by using server.user(). And after that, we do a console log so you know the mock server is running.

3. Generate Data for 100 Users

Create a folder called /users, and create an index.js in it.

First, include the npm package casual in order to use it.

For a specific language, if you don’t want the English default, type:

Then below it, we need to export a module in order to use it in the index.js in the root later.

In this case, we use server.get() for a GET request. But we also could choose post, put, delete, and so on.

Inside we check if the request was a GET request. If so, we require our user’s script and call the function inside the response so you’ll see the array of randomly generated users.

4. Create a /user Endpoint

In order to get the data for 100 users, we need to create an endpoint for the mock server. Paste the code below in the root index.js, right before the server.listen() function.

server.get(‘/users’, (request, response) => {
if (request.method === ‘GET’) {
const users = require(‘./users/index’)
response.status(200).jsonp(users())
}
})

In this case, we use server.get() for a GET request. But we also could choose post, put, delete, and so on.

Inside. we check if the request was a GET request. If so, we require our user’s script and call the function inside the response so you’ll see the array of randomly generated users.

5. Run the Mock Server

Now, we’re able to run that mock server and get the data inside our front-end application.

Run node index.js inside the root folder of the project. Visit [localhost:3000/users](http://localhost:3000/users), and you’ll see 100 users in the user array.

I hope this tutorial formed the basis for you to generate more random data and expand your mock server.

Read more

Frontend Development On A Budget: Raspberry Pi 4
medium.com

5 Tips For Healthier Developers
_Your Body And Brains Need Attention_
medium.com

What Tech I Use in 2020 As Developer
_Software, Hardware and other stuff I use during my work_
medium.com

JavaScript Concepts You Need Before Starting w/ Frameworks & Libraries
_Don’t start before you are comfortable with them_
medium.com

Did you find this article valuable?

Support Dev By RayRay by becoming a sponsor. Any amount is appreciated!