Creating the backend of a todo app
Course: MongoDB, Zod, Error handling and Input validation - chapter 7
Lets now create a todo application with the data being persisted in the database.
- Initialise a new Node.js project
npm init -y- Install dependencies
npm install express mongoose- Create the skeleton for 4 routes
- POST /signup
- POST /login
- POST /todo (authenticated)
- GET /todos (authenticated)
Solution
const express = require("express");
const app = express();app.use(express.json());
app.post("/signup", function(req, res) {
});
app.post("/signin", function(req, res) {
});
app.post("/todo", function(req, res) {
});
app.get("/todos", function(req, res) {
});
app.listen(3000);- Initialize the schema of your app in a new file (db.js)
Easy schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;const ObjectId = Schema.ObjectId;
const User = new Schema({ name: String, email: String, password: String});
const Todo = new Schema({ userId: ObjectId, title: String, done: Boolean});
const UserModel = mongoose.model('users', User);const TodoModel = mongoose.model('todos', Todo);
module.exports = { UserModel, TodoModel}Hard schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;const ObjectId = Schema.ObjectId;
const User = new Schema({ name: String, email: {type: String, unique: true}, password: String});
const Todo = new Schema({ userId: ObjectId, title: String, done: Boolean});
const UserModel = mongoose.model('users', User);const TodoModel = mongoose.model('todos', Todo);
module.exports = { UserModel, TodoModel}- Import the model in
index.js
const { UserModel, TodoModel } = require("./db");- Implement the
/signupendpoint
app.post("/signup", async function(req, res) { const email = req.body.email; const password = req.body.password; const name = req.body.name;
await UserModel.create({ email: email, password: password, name: name });
res.json({ message: "You are signed up" })});- Implement the
/signinendpoint (need to install jsonwebtoken library)
const JWT_SECRET = "s3cret";
app.post("/signin", async function(req, res) { const email = req.body.email; const password = req.body.password;
const response = await UserModel.findOne({ email: email, password: password, });
if (response) { const token = jwt.sign({ id: response._id.toString() })
res.json({ token }) } else { res.status(403).json({ message: "Incorrect creds" }) }});- Implement the
authmiddleware (in a new file auth.js)
const jwt = require("jsonwebtoken");const JWT_SECRET = "s3cret";
function auth(req, res, next) { const token = req.headers.authorization;
const response = jwt.verify(token, JWT_SECRET);
if (response) { req.userId = token.userId; next(); } else { res.status(403).json({ message: "Incorrect creds" }) }}
module.exports = { auth, JWT_SECRET}- Implement the
POSTtodo endpoint
const { auth, JWT_SECRET } = require("./auth");- Connect to your DB at the top of index.js
const mongoose = require("mongoose");mongoose.connect("mongodb+srv://gasfgfafa:Aa5jxKhylWdFhv4v@cluster0.7kmvq.mongodb.net/todo-app")