This tutorial is out of date and no longer maintained.
When building out applications, we will often need data to show off how it works. It won’t be much of an application if all our tables are blank!
Often, when we first start building out an application, we don’t want to worry too much about the data. Having to create a database and fill it with sample data like users is an annoying step that is just in the way of creating an amazing app.
Luckily there is a neat package that lets us create fake data on the fly: faker.
Let’s say we wanted to create a user on the fly.
We want this user to have a name, email, address, bio, and even an image.
In Node, all we would have to do is:
Just like that, we have a whole user object that we can use within our application! The above will spit out:
Now we have a user and can move along with our application. No need to fuss about the database or populating it with fake users!
Let’s learn a bit more about how to use this helpful package.
You can use faker from within your browser or on the server with Node.
From the docs, the installation instructions for both browser and Node are:
Super easy stuff. Let’s run through the list of items that we can pull.
There is a huge amount of data that is able to be pulled. The overall category list is:
Each of those categories has a giant amount of options within itself. We won’t go through them all, but an example is that for the name category, the following are available:
Each of these are accessible as a function. For instance:
For a giant list of all the calls that are available through faker, check out the API.
In addition to the many categories provided, faker also gives access to helpers. These are meant to make pulling data easier. Instead of picking one field here or there, these helpers can give a large amount of data that one would typically use in an application.
Here we’ll give examples of the main helpers that are usable in our own applications:
A user card that contains name, username, avatar, email, and address.
A smaller card with less user information. This one doesn’t come with a user avatar.
The full might of the user card along with a section called posts that could help simulate blog articles.
We can create a quick Node API that returns fake data. As an example, if we wanted an API endpoint ot return user data, we can create:
When our server is started, we can see this API endpoint when we use POSTman to see our data come back in JSON form.
Hopefully, this package will help you in your JavaScript development. This package helps to easily mock an API and return user information. You can get creative with it and even use this package for financial transactions.
I would encourage you to take a look at the faker homepage as well which helps you visualize the amount of data that comes out of this package.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!