Back end development

Back end development

node and Express Framework

Nowadays day tech is growing as fast as much speed of light

here we are for the basic CRUD operation Using MongoDB as a back-end node and Express as a Framework of back-end tech.

if you are reading or searching about this blog then you are a beginner in the Back end lest Deep dive into Backend code.

some basic Structure: of the controller :

this controller is used for Query or logic part in the controller function here are examples for controller

example of a controller for basic getting data and creating data

const getAllTasks = async (req, res) => { try { const getAlldata = await Task.find({}); res.status(200).json({ getAlldata });

res.status(200).json({ status: "success", data: { getAlldata, nbHits: getAlldata.length }, }); } catch (error) { res.status(500).json({ meg: Server error try again please${error} }); } };

const createTasks = async (req, res) => { try { const task = await Task.create(req.body); res.status(201).json({ task }); } catch (error) { res.status(500).json({ meg: error }); } };

as well as u can also design controller as you want for deleteTasks, updateTasks getSingleTasks, ae well

module.exports = { getAllTasks, deleteTasks, updateTasks, getSingleTasks, createTasks, };

  1. then we have to import a controller in the routes.js file and things go well

const express = require("express"); // express router setup here const router = express.Router();

const { getAllTasks, deleteTasks, updateTasks, getSingleTasks, createTasks, } = require("../controllers/TaskControler");

router.route("/").get(getAllTasks).post(createTasks) router.route('/:id').get(getSingleTasks).patch(updateTasks).delete(deleteTasks)

module.exports = router;

  1. Basic Schema for data Base for more detail can visit https://mongoosejs.com/

const mongoose = require("mongoose");

const taskSchema = mongoose.Schema({ name: { type: String, required: [true, "must provide name "], trim: true, maxlength: [20, "name can not more then 20 characters"], }, completed: { type: Boolean, default: false, }, });

module.exports = mongoose.model("Task", taskSchema);

  1. DB connection Note you have to create a .env file for DB connection for Production Grad back-end Development

const mongoose = require("mongoose");

const connectDb = (url) => { return mongoose.connect(url); };

module.exports = connectDb;

  1. all the sum up for the file in app.js or index.js file so here we are Backend app.js

//express initinalized const express = require("express"); const app = express(); const routes = require("./routes/routes"); var bodyParser = require("body-parser"); const connectDb = require("./db/connect"); //! DB CONNECTION

require("dotenv").config(); //! ENV SET UP

app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use("/api/V1/routes", routes);

//basic port declaration const port = process.env.PORT || 3737;

const start = async () => { try { await connectDb(process.env.MONGO_URI); app.listen(port, console.log(app listen on port no ${port})); console.log("connected : SERVER AND DATA BASE"); } catch (error) { console.log("CONNECTION ERROR", error); } };

start();

6 "dependencies"

this dependency requires the Backend

"dependencies": {
    "dotenv": "^16.4.5",
    "express": "^4.19.2",
    "mongoose": "^8.4.5",
    "nodemon": "^3.1.4"
 }

above are just an overview of the back-end function and code for more details about this basic crud operation visit my GitHub repository https://github.com/mehulpandya096/mehulpandya096-CRUD-API-Node.js-Express-and-MongoDB for more Basic Project https://github.com/mehulpandya096