Merced-Express: If Express Met Ruby on Rails

Creating a Todo App with Authentication

Generating the Project

Configuring Settings

DATABASE_URL=mongodb://username:password@host/databasename
SECRET=secretforjwttokens
PORT=3000

Generating The Todo Model

//----------------------------------------
// New Schema
//----------------------------------------
const TodoSchema = new Schema({
subject: String
}, { timestamps: true });

Adding Authentication

//----------------------------------------
// Import Dependencies
//----------------------------------------
import { Router } from "express";
import Todo from "../models/Todo.js";
import auth from "../config/auth.js"
//----------------------------------------
// Create Router
//----------------------------------------
const TodoRouter = Router();
//----------------------------------------
// Router Middleware
//----------------------------------------
TodoRouter.use(auth)

Testing

"Authorization":"bearer <token>"
index => get request to /todo
show => get request to /todo/:id
create => post request to /todo
update => put request to /todo/:id
destroy => delete request to /todo/:id

That’s it!

Alex Merced is a Full Stack Developer, learn more about his work at AlexMercedCoder.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store