- What is the best/most common file structure for backend NodeJS projects?
- What is the MVC architecture? Why we will be using it?
- Explore defining a schema in Mongoose, you can refer to this for more information (no answer in the comments required).
- What are the CRUD operations? add an example code of each CRUD operations in Mongoose.
-
-
Save halitbatur/4b641bd64249880151a83082f35cc5eb to your computer and use it in GitHub Desktop.
Ezgi Okur, Hafiz Mhammadah, Adnan Khaldar
What is the best/most common file structure for backend NodeJS projects?
- JSON
What is the MVC architecture? Why will we be using it?
- The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application. MVC is one of the most frequently used industry-standard web development framework to create scalable and extensible projects.
What are the CRUD operations? add an example code of each CRUD operations in Mongoose.
- The mongos instances route queries and write operations to the shards in a sharded cluster.
- Creat, Read, Update, Delete
- const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
- new Query().read('primary')
- Model.where({ _id: id }).update({ title: 'words' });
- Character.deleteOne({ name: 'Eddard Stark' }, () );
Teammates: Nilay, Rasha, Mohammad Ibrahim
What is the best/most common file structure for backend NodeJS projects?
there some rules to best orgnizing our nodejs project, we could put some here:
Rule 1 – Organize your Files Around Features, Not Roles
Rule 2 – Don’t Put Logic in index.js Files
Rule 3 – Place Your Test Files Next to The Implementation
Rule 4 – Use a config Directory
Rule 5 – Put Your Long npm Scripts in a scripts Directory
What is the MVC architecture? Why we will be using it?
Three Levels Of MVC Model :
-
Model –
This level is considered the lowest level when compared with the View and Controller. It primarily represents the data to the user and defines the storage of all the application’s data objects. -
Views –
This level is majorly associated with the User Interface(UI) and it is used to provide the visual representation of the MVC model. In simpler terms, this level deals with displaying the actual output to the user. It also handles the communication between the user (inputs, requests, etc.) and the controller. -
Controller –
This level takes care of the request handler. It is often considered as the brain of the MVC system- a link, so to speak, between the user and the system. The controller completes the cycle of taking the user output, converting it into desired messages, and passing them on to the views(UI).
and we will use it to orgnize our nodejs structure
Explore defining a schema in Mongoose, you can refer to this for more information (no answer in the comments required).
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
What are the CRUD operations? add an example code of each CRUD operations in Mongoose.
there are operations about Creating, Reading, Updating, Deleting documents in mongodb
Create ex.
router.get('/save', function(req, res) {
var newStudent = new StudentModel({StudentId:101,
Name:"Sam", Roll:1, Birthday:2001-09-08});
newStudent.save(function(err, data) {
if(err) {
console.log(error);
}
else {
res.send("Data inserted");
}
});
});
Read ex.
r```
outer.get('/findall', function(req, res) {
StudentModel.find(function(err, data) {
if(err){
console.log(err);
}
else{
res.send(data);
}
});
});
Update ex.
`router.post('/update', function(req, res) {
StudentModel.findByIdAndUpdate(req.body.id,
{Name:req.body.Name}, function(err, data) {
if(err){
console.log(err);
}
else{
res.send(data);
console.log("Data updated!");
}
});
});
`
Delete ex.
``router.get('/delete', function(req, res) {
StudentModel.remove({StudentId:188},
function(err, data) {
if(err){
console.log(err);
}
else{
res.send(data);
}
});
});
Noor Awied, Muhammed Nur, Mehmet Baki, Irem Kurt
Controller:
The controller folder should contain a route file for each data class in the system
Service Layer:
It is where the business logic live (the app’s core logic)
Data Access Layer:
This layer manage the access to the database and offer the service layer functions to access and manipulate the data.
—-----------------------------------------------------------------------------
Model–view–controller
Organize the related program logic into three interconnected components
It’s an architecture pattern rather than a design pattern
Model:
Lowest level, responsible for maintaining data, connected to the database. It responds to the controller requests because the controller never talks to the database by itself
Controller:
enables the interconnection between the views and the model so it acts as an intermediary
View:
Data representation is done by the view component. It actually generates UI or user interface for the user.
Advantages:
Decreases repetition
Separates interface from logic which increases readability.
Modularity and Reusable components
separation creates easrier maintenance and testing of components
create a solid structure of our web application
Disadvantages
Insufficient for small applications.
-The inefficiency of data access.
—-------------------------------------------------------------------------------------
3.Explore defining a schema in Mongoose, you can refer to this for more information (no answer in the comments required).
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.It defines the structure of the document, default values, validators, etc.
import mongoose from 'mongoose';
const { Schema } = mongoose;
const blogSchema = new Schema({
title: String, // String is shorthand for {type: String}
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
—-----------------------------------
CRUD stands for Create, Read, Update, Delete.
const mongoose = require("mongoose");
const FoodSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true,
lowercase: true,
},
calories: {
type: Number,
default: 0,
validate(value) {
if (value < 0) throw new Error("Negative calories aren't real.");
},
},
});
const Food = mongoose.model("Food", FoodSchema);
module.exports = Food;
—-----------------------------------
const express = require("express");
const foodModel = require("../models/food");
const app = express();
app.get("/foods", async (request, response) => {
const foods = await foodModel.find({});
try {
response.send(foods);
} catch (error) {
response.status(500).send(error);
}
});
module.exports = app;
—------------------------
// ...
app.post("/food", async (request, response) => {
const food = new foodModel(request.body);
try {
await food.save();
response.send(food);
} catch (error) {
response.status(500).send(error);
}
});
// ...
—--------------
// ...
app.patch("/food/:id", async (request, response) => {
try {
await foodModel.findByIdAndUpdate(request.params.id, request.body);
await foodModel.save();
response.send(food);
} catch (error) {
response.status(500).send(error);
}
});
// ...
—----------
// ...
app.delete("/food/:id", async (request, response) => {
try {
const food = await foodModel.findByIdAndDelete(request.params.id);
} catch (error) {
response.status(500).send(error);
}
});
// ...