Skip to content

marcelosperalta/app_fullstack_todolist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Β Β Β Β 

βœ… To-do App

A To-do App with MongoDB, Express, React, Node.js, and TypeScript.

Gitpod ready-to-code

App

to-do list

MongoDB Compass Community

to-do app db

MongoDB Atlas

to-do app db

⭐ Source

How to Build a Todo App with React, TypeScript, NodeJS, and MongoDB by Ibrahima Ndaw on freeCodeCamp.org

☁️ Getting Started Server-side

πŸ“€ Generate the tsconfig.json

yarn init -y

βž– Structure of the project

β”œβ”€β”€ server
    β”œβ”€β”€ dist
    β”œβ”€β”€ node_modules
    β”œβ”€β”€ src
        β”œβ”€β”€ controllers
        |  └── todos
        |     └── index.ts
        β”œβ”€β”€ models
        |  └── todo.ts
        β”œβ”€β”€ routes
        |  └── index.ts
        └── types
           └── todo.ts
        β”œβ”€β”€ app.ts
    β”œβ”€β”€ nodemon.json
    β”œβ”€β”€ package.json
    β”œβ”€β”€ tsconfig.json

server structure

βš™ Configuring TypeScript with tsconfig using tsc

tsc --init

Delete the tsconfig.json original settings and paste the text below:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist/js",
    "rootDir": "src",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["src/types/*.ts", "node_modules", ".vscode"]
}

β–ͺ️ outDir: tells the compiler to put the compiled code into the dist/js folder.
β–ͺ️ rootDir: informs TypeScript to compile every .ts file located in the src folder.

β–ͺ️ include: tells the compiler to include files that are in the src directory and sub-directory.
β–ͺ️ exclude: will exclude the files or folders passed in the array during compile-time.

tsconfig.json

πŸ“€ Install the dependencies to enable TypeScript

yarn add typescript -g

πŸ“€ Install the dependencies Express, CORS, and Mongoose to use Express and MongoDB

yarn add express cors mongoose

❗ install their types as development dependencies to help the TypeScript compiler understand the packages.

πŸ“’ see type declarations

yarn add -D @types/node @types/express @types/mongoose @types/cors

package.json

πŸ“€ Install the dependencies Concurrently, and nodemon

Concurrently will help compile the TypeScript code, keep watching for changes, and also start the server simultaneously.

yarn add -D concurrently nodemon

❗ update the package.json

"scripts": {
  "build": "tsc",
  "start": "concurrently \"tsc -w\" \"nodemon dist/js/app.js\""
}

package.json

#️⃣0️⃣1️⃣ Create a Todo Type

πŸ“‚ server/src/types/todo.ts

#️⃣0️⃣2️⃣ Create a Todo Model

πŸ“‚ server/src/models/todo.ts

#️⃣0️⃣3️⃣ Create API controllers

Get, Add, Update and Delete Todos

πŸ“‚ server/src/controllers/todos/index.ts

#️⃣0️⃣4️⃣ Create API routes

πŸ“‚ server/src/routes/index.ts

#️⃣0️⃣5️⃣ Create a Server

πŸ“ƒ Create a nodemon.json file to hold the MongoDB credentials.

{
    "env": {
        "MONGO_USER": "your-username",
        "MONGO_PASSWORD": "your-password",
        "MONGO_DB": "your-db-name"
    }
}

🚨 add the nodemon.json to your .gitignore file to protect your DB access data.

nodemon.json

πŸ“’ you can get the credentials by MongoDB Atlas

#️⃣0️⃣6️⃣ Create a app.ts file.

πŸ“‚ server/src/app.ts

import express, { Express } from 'express'
import mongoose from 'mongoose'
import cors from 'cors'
import todoRoutes from './routes'
import bodyParser from 'body-parser'

const app: Express = express()

const PORT: string | number = process.env.PORT || 4000

app.use(bodyParser())
app.use(cors())
app.use(todoRoutes)

// replace the host "cluster0.xo006.mongodb.net" with the address of your host generated in MongoDB Atlas.
// https://docs.mongodb.com/manual/reference/connection-string/
const uri: string = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.xo006.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`

const options = { useNewUrlParser: true, useUnifiedTopology: true }
mongoose.set('useFindAndModify', false)

mongoose
.connect(uri, options)
.then(() =>
    app.listen(PORT, () =>
        console.log(`Server running on http://localhost:${PORT}`)
    )
)
.catch((error) => {
    throw error
})

🚨 about the const uri above, you need to change the cluster url cluster0-shard-00-01.xo006.mongodb.net using your own cluster url generated by MongoDB Atlas.

πŸ’» Client-side with React and TypeScript

βš™ Setting up

β–ͺ️ Create a new React App adding TypeScript

🚨 the client folder needs to be at the same level of the server folder.

client folder

npx create-react-app client --template typescript

β–ͺ️ open the client folder.

cd client

β–ͺ️ Install the Axios library to be able to fetch remote data.

yarn add axios

βž– Structure of the project

β”œβ”€β”€ client
    β”œβ”€β”€ node_modules
    β”œβ”€β”€ public
    β”œβ”€β”€ src
    |   β”œβ”€β”€ components
    |   |  β”œβ”€β”€ AddTodo.tsx
    |   |  └── TodoItem.tsx
    |   β”œβ”€β”€ API.ts
    |   β”œβ”€β”€ App.tsx
    |   β”œβ”€β”€ App.test.tsx
    |   β”œβ”€β”€ index.css
    |   β”œβ”€β”€ index.tsx
    |   β”œβ”€β”€ react-app-env.d.ts
    |   β”œβ”€β”€ setupTests.ts
    |   └── type.d.ts
    β”œβ”€β”€ tsconfig.json
    β”œβ”€β”€ package.json
    └── yarn.lock
β”œβ”€β”€ server

client structure

#️⃣0️⃣1️⃣ Create a Todo Type

πŸ“‚ client/src/type.d.ts

#️⃣0️⃣2️⃣ Fetch data from the API

πŸ“‚ client/src/API.ts

#️⃣0️⃣3️⃣ Add Todo Form

πŸ“‚ client/src/components/AddTodo.tsx

#️⃣0️⃣4️⃣ Display a Todo

πŸ“‚ client/src/components/TodoItem.tsx

#️⃣0️⃣5️⃣ Fetch and Display data

πŸ“‚ client/src/App.tsx

πŸš€ Run the Project

:octocat: Git clone

git clone https://github.com/marcelosperalta/todoApp_react

client

☁️ Server-side

β–ͺ️ Open server folder

cd server

β–ͺ️ Create nodemon.json file (e.g. using PowerShell)

New-Item nodemon.json

β–ͺ️ Fill the nodemon.json file with MongoDB credentials

{
    "env": {
        "MONGO_USER": "your-username",
        "MONGO_PASSWORD": "your-password",
        "MONGO_DB": "your-db-name"
    }
}

client

β–ͺ️ Install the project dependencies based on the package.json

yarn install

client

β–ͺ️ Run the project based on the package.json start script

yarn start

🚨 If you found some error during the first start, stop the app (ctrl + c) and try to run again.

client

πŸ’» Client-side

β–ͺ️ Open client folder

cd client

β–ͺ️ Install the project dependencies based on the package.json

yarn install

client

β–ͺ️ Run the project based on the package.json start script

yarn start

client

πŸƒ Run ☁️ Server-side and πŸ’» Client-side simultaneously

β–ͺ️ Open server folder

cd server

β–ͺ️ Create nodemon.json file (e.g. using PowerShell)

New-Item nodemon.json

β–ͺ️ Fill the nodemon.json file with MongoDB credentials

{
    "env": {
        "MONGO_USER": "your-username",
        "MONGO_PASSWORD": "your-password",
        "MONGO_DB": "your-db-name"
    }
}

client

β–ͺ️ Install the project dependencies based on the package.json

yarn install

β–ͺ️ Open client folder

cd client

β–ͺ️ Install the project dependencies based on the package.json

yarn install

client

β–ͺ️ Return to the root folder of the project and install the dependencies based on the package.json

yarn install

β–ͺ️ Run the project based on the package.json start script

yarn start