title: Generate fake data in Node.js using Faker.js library layout: page
The code in this page repository will allow you to automatically generate Fake Data using Faker.js in Node.js. It then will save it to your local MongoDB and display the results on a webpage. Feel free to use the code as a template for your code.
- Folder Structure
- How the project will run
- [Setting up the application starting point](#Setting-up-the-application-starting point)
- Setting up views
- Setting up models
- Run the project
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
And that is all! Feel free to contribute to the repository as you see well...
Author: JustusMbuvi