A To-do App with MongoDB, Express, React, Node.js, and TypeScript.
App
MongoDB Compass Community
MongoDB Atlas
How to Build a Todo App with React, TypeScript, NodeJS, and MongoDB by Ibrahima Ndaw on freeCodeCamp.org
yarn init -y
βββ 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
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.
π Install the dependencies to enable TypeScript
yarn add typescript -g
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
π 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\""
}
π server/src/models/todo.ts
Get, Add, Update and Delete Todos
π server/src/controllers/todos/index.ts
π server/src/routes/index.ts
π 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.
π’ you can get the credentials by MongoDB Atlas
π 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.
βͺοΈ Create a new React App adding TypeScript
π¨ the client
folder needs to be at the same level of the server
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
βββ 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/src/type.d.ts
π client/src/API.ts
π client/src/components/AddTodo.tsx
π client/src/components/TodoItem.tsx
π client/src/App.tsx
git clone https://github.com/marcelosperalta/todoApp_react
βͺοΈ 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"
}
}
βͺοΈ Install the project dependencies based on the package.json
yarn install
βͺοΈ 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.
βͺοΈ Open client
folder
cd client
βͺοΈ Install the project dependencies based on the package.json
yarn install
βͺοΈ Run the project based on the package.json
start script
yarn start
βͺοΈ 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"
}
}
βͺοΈ 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
βͺοΈ 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