Generate fake data using Faker.js in a Node application. generated from here
- Initialize project
- Configure nodemon in package.json
- Add a starting point
- Add models folder
- Add a views folder
- Start MongoDB
- Run the project
View the steps in the webpage found here.
The folder structure will be as shown below:
Fakedata (Root Directory)
-- node_modules (folder)
-- models (folder)
-- user.js (file)
-- views (folder)
-- home.ejs (file)
-- app.js (file)
The code when executed will use the faker.js library to generate the data needed several times. It will store each value generated in a local database. We shall then see the results in a web browser as a webpage from the database.
Clone this repository on your machine.
The node libraries required for this tutorial include:
- nodemon
- express
- ejs
- mongoose
- faker
In the integrated terminal, run the following command to install the above packages all at once:
npm i nodemon express ejs mongoose faker
Now open the “package.json” file and configure it so that you can start the application using nodemon. Under the scripts, add the “dev” and “start” configurations. The code should look as follows:
{
"name": "fakedata",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^2.7.4",
"express": "^4.17.1",
"faker": "^4.1.0",
"mongoose": "^5.12.5",
"nodemon": "^2.0.7"
}
}
Before running the code, make sure that MongoDB is running in the background.
In your integrated terminal, run the following command: nodemon run dev