0% found this document useful (0 votes)
122 views6 pages

NodeJS CRUD API with MongoDB Setup

The document provides instructions for setting up a Node.js application to build a CRUD API connected to a MongoDB database in 10 steps. It then describes adding additional functionality like connecting to MongoDB, creating a user model and controller, building routes to get, create, and read all users, and editing a user record. The steps include initializing the project, installing dependencies, setting up the server, routing, and controllers to manage the CRUD operations for a user collection in the MongoDB database.

Uploaded by

hatemzomor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views6 pages

NodeJS CRUD API with MongoDB Setup

The document provides instructions for setting up a Node.js application to build a CRUD API connected to a MongoDB database in 10 steps. It then describes adding additional functionality like connecting to MongoDB, creating a user model and controller, building routes to get, create, and read all users, and editing a user record. The steps include initializing the project, installing dependencies, setting up the server, routing, and controllers to manage the CRUD operations for a user collection in the MongoDB database.

Uploaded by

hatemzomor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Fall 2023

Advanced Full Stack Web Programming


Lab 1 - Setting up NodeJS App

A. Create new project folder (same as Step 1 and 2)


1- mkdir crud-API
2- cd crud-API
3- npm init -y
4- npm install mongoose express dotenv cors nodemon
5- create new file [Link]
const express = require("express");
const PORT = 8000;
const app = express();
[Link](PORT, async () => {
[Link](`server up on port ${PORT}`);
});
6- [Link] (under scripts “,”)
"start": "nodemon [Link]"
7- npm run start (in terminal of [Link])
8- [Link]
const router = require("express").Router();
[Link]("/", (req, res) => {
[Link]("Let's build a CRUD API!");
});
[Link] = router;
9- Open [Link] file add the routing object
const router = require("./router");
[Link](router); (at the end of the file)

10- [Link]

pg. 1 [Link]
Fall 2023

B. Connect to MongoDB
1- Create Project
2- Create User and password (remember it well!)
Example:
raghdaessam1
R123456
3- Click Network access – Add new IP address – allow access from any IP
4- Click connect to Database
5- Click Drivers – Copy connection (Next time you will choose Resume only)
6- Create new .env file
7- Make a new variable and Copy connection string in .env and replace your password
8- Open [Link] file, create and configure .env database connection

const mongoose = require("mongoose");


const dotenv = require("dotenv");
[Link]();

------------------------------------------------------------------------------
mongoose
.connect([Link].MONGODB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
[Link]("Connected to MongoDB");
})
.catch((err) => {
[Link](err);
});

pg. 2 [Link]
Fall 2023

Create Model for Mongo


1- Create Model
const mongoose = require("mongoose");

const UserSchema = new [Link]({


first_name: {
type: String,
required: true,
},
last_name: {
type: String,
required: true,
},
user_name: {
type: String,
required: true,
},
email: {
type: String,
},
password: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: [Link],
},
});

[Link] = [Link]("User", UserSchema);

2- Create Controller
const getUsers = (req, res) => {
[Link]("I am the get users route");
};

[Link] = {
getUsers,
};

a. [Link]
const {getUsers}= require("./Controllers/User");
[Link]("/users", getUsers);
3- [Link]

pg. 3 [Link]
Fall 2023

Build Create API


1- Add post to router
[Link]("/users", createUser);
2- Adjust import
const {getUsers,createUser}= require("./Controllers/User");
3- Add Api to controller
const createUser = async (req, res) => {
const user = new User({
first_name: [Link].first_name,
last_name: [Link].last_name,
user_name: [Link].user_name,
email: [Link],
password: [Link],
createdAt: [Link],
});

try {
const savedUser = await [Link]();
[Link](savedUser);
} catch (err) {
[Link](err);
}
};
4- Export function
[Link] = {
getUsers,
createUser,
};

5- Add cors to [Link]


const cors = require("cors");
[Link](cors());

[Link]([Link]());
[Link]([Link]({ extended: false }));
6- Open Postman
{
"first_name": "Raghda",
"last_name": "Ali",
"user_name": "raghda_ali",
"email": "[Link]@[Link]",
"password": "1234567"
}

pg. 4 [Link]
Fall 2023

Create Read All


const getUsers = (req, res) => {
[Link]()
.then(users => [Link](users))
.catch(err => [Link](err));
};

pg. 5 [Link]
Fall 2023

Edit User
1- Add to [Link]

pg. 6 [Link]

You might also like