Build a TODO app in less than 5 minutes in Node/Express
This looks crazy I know. Without wasting any time, let us list down the components or building blocks for our TODO App.
- Express Js framework
- MySQL as database
- Knex Query Builder, and Objection ORM
- CRUD API Generator for Express for faster API generation
Our todo app will be hosted on localhost:3000, and it will provide us the following endpoints
- POST request to /todos to create a new Todo item
- GET request to /todos/:id to get a Todo item by id
- PUT request to /todos/:id to update a Todo item by id
- DELETE request to /todos/:id to delete a Todo item by id
- GET request to /todos to get all the Todo items
Pre-requisites
- You have Node and Npm set up on your computer
- You have a MySQL server up and running
- Install dricup-crud-express generator
To do this, open up the terminal or command prompt and run the following command
npm install -g @farazahmad759/dricup-crud-express
Source code: https://github.com/farazahmad759/dricup-crud-express/tree/main/examples/hello-todo
Getting Started
Okay, so let’s dive into the delicious cloud of development ;)
Step 1: Project bootstrap
- Create a new directory
hello-todo
in your working machine. - Open this directory in Visual Studio Code (VSCode) and bring its integrated terminal.
- Next, copy and run the following command
npx express-generator
This will generate an express-starter app for you with all the directories and start-up code set up for you in seconds.
Now run npm install
to install all the dependencies mentioned in the file. After all the dependencies have been installed, you can run npm start
and visit the newly created Express App at localhost:3000
.
Step 2: Set up the CRUD Generator
- Create a new directory
db
in the root of your project. - Create another directory
schemas
insidedb
. - Now create a
todos.json
and ausers.json
file insidedb/schemas
the directory, and paste the below codes into respective files.
- The interesting part starts here. Run
dricup-crud-express
command in the terminal and see the magic. It will create Migration, Model, Controller, and Routes for the todo’s along with some configuration files.
Step 3: Configure Database
The commanddricup-crud-express
above has created an knexfile.js
in the root of the project.
- Open it up and add your database details for
development
environment/key. - Run
knex migrate:up
to run migrations.
Step 4: Update `app.js` file and restart server
Open the app.js
file and add the following two lines to it
var todosRouter = require("./routes/todos");app.use("/todos", todosRouter);
Now close the server (if it is already running) and run the command npm start
.
Step 5: Enjoy the App using Postman
Open postman, and you can create/read/update/delete the todos :)
- POST request to /todos to create a new Todo item
- GET request to /todos/:id to get a Todo item by id
- PUT request to /todos/:id to update a Todo item by id
- DELETE request to /todos/:id to delete a Todo item by id
- GET request to /todos to get all the Todo items