Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

## Building a simpile crud app => its a backend project # Approch => - need to see a list of all existing tasks on the screen. - type a new task into the input box and click "Submit" to create it. - click an "Edit" icon to open a single task's details. - update the task name or check the "Completed" box. - click a trash can icon to delete a task. # how to identify which routes i need | User Goal | HTTP Method | Route / URL Path | Purpose | | :------------------- | :------------ | :------------------ | :----------------------------------------------------------- | | **Get all tasks** | `GET` | `/api/v1/tasks` | Fetches the entire list to display on the home page. | | **Create a task** | `POST` | `/api/v1/tasks` | Sends the new task data from the input box to the database. | | **Get a single task** | `GET` | `/api/v1/tasks/:id` | Fetches just one specific task when clicking "Edit". | | **Update a task** | `PATCH`/`PUT` | `/api/v1/tasks/:id` | Saves the edits or toggles completeness for a specific task. | | **Delete a task** | `DELETE` | `/api/v1/tasks/:id` | Removes a specific task from the database. | # SETUP 1. npm init -y 2. npm install express dotenv mongoose[ DEPENDICE- NEEDED TO RUN THE PROJECT] 3. npm install nodemon --save-dev [ DEV-DEPENDICE NEED IN DEVLOPMENT ] 4. modify pkg.json "scripts": { "dev": "nodemon app.js" }, => npm run dev # making a route for testing - FLOW Client Request ↓ app.get('/hello') ↓ (req, res) ↓ res.send(...) ↓ Response back to client rest-> pattern of making api ,:id -> route params ;specfic id req.params->Gets data from the URL path. req.body->Gets data sent inside the request body. also if user visit /tasks/123 => req parms => object containg id:123 and req.params.id => 123 if user /tasks and send { 'name':"learn express" 'completed':false } then req.body usually used in post { name: "Learn Express", completed: false } spelling sub same in patch both used params → "Which one?" body → "What data?" req (request) is a JavaScript object created by Express that contains information about the incoming request. From query strings: /tasks?completed=true&sort=name req.method console.log(req.method) Output: GET POST PATCH DELETE req.url console.log(req.url) Output: /tasks/123 Client ----request----> Server req Server ----response---> Client res # now after with the basic just make controller and route for all items and add middleware to it to see its working properly app.use() is how you add "Plugins" (called Middleware) to your Express application.app.use() plugs new machines onto that conveyor belt to process the request before it reaches your final routes. When you run into a crash, the Node.js terminal prints out what we call a Stack Trace. It looks like a big block of scary text, but it's actually a roadmap telling you exactly where the code broke.=> i need to study about this. # so upto here the normal flow is => Express App ↓ Routes ↓ Controllers But everything was returning fake order. res.json(req.body) res.json({ id: req.params.id }) There was no database yet. Then Mongoose enters the picture. What is Mongoose? Mongoose is a library that helps Node.js talk to MongoDB. with mongoose => Node.js → Mongoose → MongoDB It provides: - Database connection - Schemas - Models - Validation - CRUD operations # In short -> Connected Express app to MongoDB using Mongoose. Created a separate db/connect file. Imported it in app.js using require('./db/connect'). require() means: "Load and execute another file." => we just need to run this file as we enter in app.js or look into it first thing get databse into this file MongoDB = Database Mongoose = Library that lets Node.js talk to MongoDB # now theres a problem with our setup Problem with the basic setup Your note says something like: Server and database connection are not in sync What if MongoDB fails to connect? MongoDB ❌ But the server still starts: Server listening on port 3000 Now users can hit your API, but your database isn't connected. # so better approch -> First connect to the database. Connect DB ↓ Success? ↓ Start Server# PROJECTS

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages