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
- 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
- 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
const express = require("express");
const app = express();
const port = 7070;
app.listen(port, () => {
console.log(`Server running... , http://localhost:${port}`);
});
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}`);
})
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
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 asreq.params
,req.query
,req.body
, andreq.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, includingres.send()
,res.json()
, andres.render()
. -
You can also set HTTP response headers using methods like
res.set()
and set the HTTP status code usingres.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.
Respond with Hello World! on the homepage:
app.get('/', (req, res) => {
res.send('Hello World!')
})
Respond to POST request on the root route (/), the applicationβs home page:
app.post('/', (req, res) => {
res.send('Got a POST request')
})
Respond to a PUT request to the /user route:
app.put('/user', (req, res) => {
res.send('Got a PUT request at /user')
})
Respond to a DELETE request to the /user route:
app.delete('/user', (req, res) => {
res.send('Got a DELETE request at /user')
})
-
create account in mongoDb database
-
create deployment
- enter cluster username end password.
-
Click connect
-
Click Drives end copy the link
- 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
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 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}`);
})
- 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
- check mongodb database cluster data is added.
/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);
- click get
- enter url with path
/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
- 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
- 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)
- 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)
- 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)
- 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
- install react
npm create vite ./
- install modules
npm i
- install axios for connect backend
npm i axios
MIT License : Copyright (c) 2023 Dev_eLoper