Create a project folder and Inside your project folder type
npm init -y npm init -y // it will create a package.json file
{
“name”: “crud-api-task”,
“version”: “1.0.0”,
“description”: “crud api using nodejs”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“keywords”: [],
“author”: “”,
“license”: “ISC”
}
–: NOW to add express and mongoose library use command
npm install express — save
npm install mongoose — save
3. Now create a file app.js and write the following code
const express = require(`express`);
const app = express();
app.listen(3000, () => {
console.log(“Server started on port 3000”);
});
And run command
node app.js // output will be Server started on port 3000 http://localhost:3000/
4- Now create a folder inside your project folder named database and create a file mongoose.js
database/mongoose.js
5- create one folder inside database folder named models and two files inside models folder
database/models/taskList.js
database/models/task.js
6:- Now get the path of mongodb server in your system by typing following command
rani@RANIs-MacBook-Pro ~ % mongosh
Current Mongosh Log ID: 670bfd726167bd96c190e5c5
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.3.2
Using MongoDB: 8.0.1
Using Mongosh: 2.3.2
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
And write the following code in mongoose.js file
const mongoose = require(`mongoose`);
mongoose.Promise =global.Promise;
mongoose.connect(`mongodb://127.0.0.1:27017/taskmanagerdb`)
.then(() => {
console.log(“DB Connected Successfully”)
})
.catch((error) => {
console.log(error)
});
module.export = mongoose;
And modify app.js to
const express = require(`express`);
const app = express();
const mongoose = require(`./database/mongoose`);
app.listen(3000, () => {
console.log(“Server started on port 3000”);
});
And run command
node app.js
// output willl be Server started on port 3000 √ DB Connected Successfully
6-: NOW WE WILLL install nodemon — using it we did not need to run the command node app.js again and again to refresh the page .
nodemon will do it . so install the dependency using
npm install nodemon — save — include=dev
here is the github repo
https://github.com/sudeshgoyat20/crud-api-task/tree/sudesh/feature/dev