Skip to content

Learning Node Js end Express Js with MongoDb Data base end improve your as a Backend Developer. πŸŽ“

License

Notifications You must be signed in to change notification settings

webDev5464/NodeExpressJs-README

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŽ“ Node Js + Express Js + MongoDb

βš™οΈ Dependency Install

Link description
Node Js Install Node Js for backend.
MongoDb Install MongoDb software for database.
Postman Install Postman software for debugging

Postman also available in Vs Code as Extension. Search Postman

  • Now open powershell as administer.

Install nodemon in your system

  • Way install nodemon ?

:- Auto reloading server after changing code.

npm i nodemon -g
Set-ExecutionPolicy Unrestricted

πŸ“Œ Node end Express Install

  • mkdir myBackend
  • cd myBackend

Now install Express in the myBackend directory and save it in the dependencies list. For example:

npm init
npm i express

πŸ“Œ Run as a localhost.

  • create new file index.js.
const express = require('express')
const app = express()

const port = 8080
app.listen(port, () => {
  console.log(`Server Started : http://localhost:${port}`);
})

let's run in browser. nodemon for starting server.

PS D:\NodeExpress\backend> nodemon

πŸŽ‰ Congratulation: Your local host started.

index.js

πŸ“Œ Create server

const express = require("express");
const app = express();

const port = 7070;
app.listen(port, () => {
  console.log(`Server running... , http://localhost:${port}`);
});

πŸ“Œ Create home page API

index.js

const express = require('express')
const app = express()

/* ----- */
app.get('/', (req, res) => {
  res.send("Hello Express")
})
/* ----- */

const port = 8080
app.listen(port, () => {
  console.log(`Server Started : http://localhost:${port}`);
})

πŸ”Ί multiple API

index.js

const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.send("Hello Express")
})

app.get('/about', (req, res) => {
  res.send("This is a about page")
})

const port = 8080
app.listen(port, () => {
  console.log(`Server Started : http://localhost:${port}`);
})

change localhost path end check output :- http://localhost:7070/about

πŸ“Œ What is req (request) & res (response) ?

In Express.js, "request" and "response" are two fundamental objects used when handling HTTP requests and generating HTTP responses in your web applications. They represent the incoming request made by a client (usually a web browser) and the outgoing response that your server sends back to the client. Here's what each object represents:

**1. ** Request (req):

  • The req object represents the HTTP request made by the client. It contains information about the incoming request, such as the URL, HTTP method (GET, POST, etc.), request headers, request parameters, and any data sent in the request body.

  • You can access various properties and methods on the req object to retrieve data from the request, such as req.params, req.query, req.body, and req.headers.

  • You can also use req to perform tasks like authentication, validation, and processing the incoming data.

**2. ** Response (res):

  • The res object represents the HTTP response that your Express application will send back to the client. It's used to set response headers, send data to the client, and manage the response status.

  • You can use methods and properties on the res object to send data back to the client, including res.send(), res.json(), and res.render().

  • You can also set HTTP response headers using methods like res.set() and set the HTTP status code using res.status().

Here's a simple example of how you might use req and res in an Express route handler:

app.get('/example', (req, res) => {
  // Access request data
  const queryParam = req.query.paramName;
  
  // Send a response to the client
  res.status(200).send('Hello, World!');
});

In this example, the req object is used to access the query parameter paramName from the URL, and the res object is used to send the "Hello, World!" response with an HTTP status code of 200.

These objects are essential for building web applications in Express.js as they allow you to handle incoming requests and send appropriate responses to the client based on those requests.

πŸ”Ί get method

Respond with Hello World! on the homepage:

app.get('/', (req, res) => {
  res.send('Hello World!')
})

πŸ”Ί post method

Respond to POST request on the root route (/), the application’s home page:

app.post('/', (req, res) => {
  res.send('Got a POST request')
})

πŸ”Ί put method

Respond to a PUT request to the /user route:

app.put('/user', (req, res) => {
  res.send('Got a PUT request at /user')
})

πŸ”Ί delete method

Respond to a DELETE request to the /user route:

app.delete('/user', (req, res) => {
  res.send('Got a DELETE request at /user')
})

πŸ“Œ Connect With mongoDb

  • create account in mongoDb database

  • create deployment

CreateDeployment

  • enter cluster username end password.

clusterUsernamePassword

  • Click connect

  • Click Drives end copy the link

connecting database

  • install mongoose
npm i mongoose

Create new dir configs end file databaseConnection.js.

/configs/databaseConnection.js

const { default: mongoose } = require("mongoose");

mongoose.connect("mongodb+srv://readme:readme@nodeexpress-readme.5sz7pau.mongodb.net/?retryWrites=true&w=majority").then(() => {
  console.log("Database Connected...");
})

require in index.js

index.js

// Some Code
require('./configs/databaseConnection')
// Some Code

πŸ“Œ postData in mongodb.

πŸ”Ί Create models

Create new dir models end new file createData.js

create model in database.

/models/createData.js

const mongoose = require("mongoose");

const createData = mongoose.Schema({
  title: String,
});

const dataCreated = mongoose.model("createdData", createData);

module.exports = dataCreated;

πŸ”Ί create controllers (API)

Create new dir controllers end new file postData.js

data detail end save data in mongodb.

/controllers/postData.js

const createData = require("../models/createData.js");

const postData = async (req, res) => {
  console.log(req.body);
  const data = createData({
    title: req.body.title,
  });

  await data.save();
};

module.exports = postData;

Now require end use.

index.js

const express = require('express')
const app = express()

// is require
// database side data store in json formate.
app.use(express.json())

// require API
const postData = require("./controllers/postData.js");

app.get('/', (req, res) => {
  res.send("Server is live 🟒")
})

// use API with post method
app.post("/data", postData);

const PORT = 8080
app.listen(PORT, () => {
  console.log(`Server Connected : http://localhost:${PORT}`);
})

πŸ”Ί postData with postman

  • Create a new collection.
  • select post method
  • Enter localhost url end path.
  • Select body > row > json
  • add json form data
  • end send
  • sending process is continue running than your data is sending successfully

postman with post data

  • check mongodb database cluster data is added.

data is successfully posted

πŸ”Ί getData method

/controllers/getData.js

const createData = require("../models/createData");

const getData = async (req, res) => {
  let data = await createData.find({});
  console.log(data);
  res.send({ process: true, data });
};

module.exports = getData;

index.js

const getData = require("./controllers/getData.js");
// Some Code
app.get("/getData", getData);

πŸ”Ί getData with postman

  • click get
  • enter url with path

get method in postman

πŸ”Ί deleteData method

/controllers/deleteData.js

const createData = require("../models/createData");

const deleteData = async (req, res) => {
  const id = req.params.id
  await createData.findByIdAndDelete(id)
}

module.exports = deleteData

πŸ“Œ createData, postData, getAllData end getCategoryData method

πŸ”Ί createData method

  • create a new products models

models/createProductData.js

const mongoose = require("mongoose")

const createProductData = mongoose.Schema({
  category: String,
  brand: String,
  img_1: String,
  img_2: String,
  img_3: String,
  title: String,
  weight: String,
  price: Number,
  discount: Number
})

const productData = mongoose.model("productData", createProductData)

module.exports = productData

πŸ”Ί postData method

  • create new controllers

/controllers/postProductData.js

const createdProductData = require("../models/createProductData")

const postProductData = async (req, res) => {
  console.log(req.body);
  const data = createdProductData({
    category: req.body.category,
    brand: req.body.brand,
    img_1: req.body.img_1,
    img_2: req.body.img_2,
    img_3: req.body.img_3,
    title: req.body.title,
    weight: req.body.weight,
    price: req.body.price,
    discount: req.body.discount,
  })

  await data.save()
}

module.exports = postProductData

index.js

const postProductData = require("./controllers/postProductData.js");
// Some Code
app.post("/postProductData", postProductData)

πŸ”Ί getAllData method

  • create new controllers

/controllers/getProductData.js

const createdProductData = require("../models/createProductData")

const getProduct = async (req, res) => {
  let data = await createdProductData.find({})
  console.log(data);
  res.send({ process: true, data })
}

module.exports = getProduct

index.js

const getProduct = require("./controllers/getProductData.js");
//Some Code
app.get("/getAllProducts", getProduct)

πŸ”Ί getCategoryData method

  • create new controllers

/controllers/getProductsByCategory.js

const productData = require("../models/createProductData");

let getProductsByCategory = async (req, res) => {
  let productCategory = await productData.find({ category: req.params.products })
  console.log(productCategory);
  res.send({ process: true, productCategory })
}

module.exports = getProductsByCategory

index.js

const getProductsByCategory = require("./controllers/getProductsByCategory.js");
// Some Code
app.get("/products/category/:products", getProductsByCategory)


πŸŽ“ NodeJs, ExpressJs $ MongoDb Connect with react

βš™οΈ backend side installation dependency.

  • init
npm init
  • install express
npm i express
  • install mongoose for connect with database
npm i mongoose
  • install cors for connect with frontend
npm i cors

βš™οΈ frontend side installation dependency.

  • install react
npm create vite ./
  • install modules
npm i
  • install axios for connect backend
npm i axios

Status Code




πŸͺͺ License

MIT License : Copyright (c) 2023 Dev_eLoper

About

Learning Node Js end Express Js with MongoDb Data base end improve your as a Backend Developer. πŸŽ“

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published