How to Quickly Mock an API Server in a Client-Side Project

Jeremy Wells
Jan 15 · 7 min read
Image for post
Image for post
Photo by Lars Kienle on Unsplash

Introduction

Prerequisites

$ mkdir file_viewer
$ cd field_viewer
$ touch index.html db.json

Step one: Data modeling

id: number
fileName: string
fileType: string
createdDate: Date
email: string
active: boolean
{
"id": 30,
"fileName": "The Chrysanthemum and the Dandelion",
"fileType": ".pdf",
"createdDate": "2020-11-18T01:36:46Z",
"email": "kuni@garu.com",
"active": true
}

Step two: Generate mock data with Mockaroo

Image for post
Image for post
['.docx', '.xlsx', '.pdf'].sample
Image for post
Image for post

Serving the mock data with json-server

$ npm install -g json-server
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
$ json-server --watch db.json
{ "id": 1, "title": "json-server", "author": "typicode" }
{
"files": []
}
{
"files": [
{"id":1,"fileName":"nulla.xls","fileType":".xlsx","createdDate":"2020-02-27T05:10:27Z","email":"hweiner0@wikimedia.org","active":true},
{"id":2,"fileName":"sapien urna.png","fileType":".xlsx","createdDate":"2020-12-20T05:12:00Z","email":"creding1@google.com.br","active":false},
{"id":3,"fileName":"vel augue.ppt","fileType":".docx","createdDate":"2020-10-03T05:06:18Z","email":"ccrust2@theguardian.com","active":true},
{"id":4,"fileName":"mauris morbi non.txt","fileType":".docx","createdDate":"2020-04-26T13:44:41Z","email":"nfolbig3@livejournal.com","active":true},
{"id":5,"fileName":"sapien sapien non.ppt","fileType":".pdf","createdDate":"2020-04-16T17:56:06Z","email":"cdesimoni4@uol.com.br","active":true},
// ... ]
}
Image for post
Image for post
Image for post
Image for post
<!DOCTYPE html>
<html>
<head>
<title>File Viewer</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>File Viewer</h1>
<script>
fetch('http://localhost:3000/files')
.then(response => response.json())
.then(data => console.log('Data from our mock backend: ', data));
</script>
</body>
</html>
Image for post
Image for post

Conclusion

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Jeremy Wells

Written by

I am a second-career software engineer currently working in the public sector.

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Jeremy Wells

Written by

I am a second-career software engineer currently working in the public sector.

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store