Introduction
What is faker.js?
- Faker.js is a javascript library for generating fake data.
- Fake data is useful when building and testing our application.
- The faker.js can generate fake data for various areas, including address, name, email, product, etc.
- For more details, visit faker.js.
Project Structure
|----------- app.js|||--------- models| |---------- user.js||--------- views|---------- home.ejs
How to set up the Project Folder
1. Open console/terminal and type mkdir followed by the folder name you want to give.
2. Change to the new folder by typing cd folder name.
Setup Node For The Project
1. Type npm init on console/terminal.
2. The npm init will create a package.json file
3. The package.json will look like this:
4. Now we will install the packages for our project using npm install followed by package names we want to install.
5. The following packages will be installed: express ejs faker mongoose.
6. This will generate a folder name node_modules which will contain all the node packages.
7. After the package installation, the package.json will look like this:
7. After the package installation, the package.json will look like this:
8. Now add a new file named app.js.
9. Go back to package.json file, and in scripts write start :app.js.
9. Go back to package.json file, and in scripts write start :app.js.
10. This will be the application entry point.

11. Make new folders views and models.
12. In views add a file home.ejs.
13. Here, we are using .ejs extension for views file because the template engine is ejs.
13. Here, we are using .ejs extension for views file because the template engine is ejs.
14. Add a file user.js.
15. User.js will contain the collection (table) schema.
Models
- var mongoose = require('mongoose')
- var fakerSchema = new mongoose.Schema({
- firstname: String,
- lastname:String,
- phonenumber:String,
- city:String,
- state:String,
- country:String
- });
- module.exports = mongoose.model('fakeCollection',fakerSchema);
- mongoose.Schema({}) - contains the fields of our collection(table).
- mongoose.model() - contains collection(table) name and the object containing collection(table) the field data.
- fakeCollection is the collection(table) name.
- module.exports - makesthis module accessible in other module/files. We have to export it using this.
app.js
Join the conversation! Your thoughts help the community grow.