- Create
todo-api
folder - Add or copy
package.json
file - create
src
foler insidetodo-api
folder - create
server.js
andTodo.js
file insidesrc
folder - And run
npm install
in the root directory i.e.todo-api
- run
npm start
Created
October 18, 2021 06:27
-
-
Save prakash-pun/08b8f74582eb9b8b7bbf52588b61c760 to your computer and use it in GitHub Desktop.
Todo App API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "todo-api", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "nodemon src/server.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "1.19.0", | |
"cors": "2.8.5", | |
"express": "4.17.1", | |
"mongoose": "^6.0.9", | |
"nodemon": "^2.0.13", | |
"uuid": "3.3.3" | |
}, | |
"devDependencies": { | |
"@babel/core": "7.7.5", | |
"@babel/node": "7.7.4", | |
"@babel/preset-env": "7.7.6" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const cors = require("cors"); | |
const bodyParser = require("body-parser"); | |
const mongoose = require("mongoose"); | |
const Todo = require("./Todo"); | |
const MONGO_OPTIONS = { | |
useUnifiedTopology: true, | |
useNewUrlParser: true, | |
socketTimeoutMS: 30000, | |
keepAlive: true, | |
autoIndex: false, | |
retryWrites: false, | |
}; | |
mongoose | |
.connect("mongodb://localhost/todo-app", MONGO_OPTIONS) | |
.then((result) => { | |
console.log("database connected"); | |
}) | |
.catch((error) => { | |
console.log(error); | |
}); | |
const app = express(); | |
app.use(bodyParser.json()); | |
app.use(cors()); | |
app.get("/todos", async (req, res) => { | |
const todos = await Todo.find({}); | |
res.status(200).json(todos); | |
}); | |
app.get("/todos-delay", async (req, res) => { | |
const todos = await Todo.find({}); | |
setTimeout(() => res.status(200).json(todos), 2000); | |
}); | |
app.post("/new", async (req, res) => { | |
const { text } = req.body; | |
if (text) { | |
const todo = await Todo.create({ text: text }); | |
todo.save(); | |
res.status(200).json(todo); | |
} else { | |
res.status(400).json({ | |
message: "Request body should have a text property", | |
}); | |
} | |
}); | |
app.post("/todos/:id/completed", async (req, res) => { | |
const { id } = req.params; | |
const todo = await Todo.findById(id); | |
if (todo) { | |
todo.isCompleted = true; | |
todo.save(); | |
res.status(200).json(todo); | |
} else { | |
res.status(400).json({ | |
message: "There is no todo with that id", | |
}); | |
} | |
}); | |
// The route for deleting a todo-list item | |
app.delete("/todos/:id", async (req, res) => { | |
const { id } = req.params; | |
const todo = await Todo.findByIdAndDelete(id); | |
res.status(200).json(todo); | |
}); | |
app.listen(8080, () => console.log("Server listening on port 8080")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { model, Schema } = require("mongoose"); | |
const todoSchema = Schema({ | |
text: { | |
type: String, | |
required: true, | |
min: 4, | |
}, | |
isCompleted: { | |
type: Boolean, | |
default: false, | |
}, | |
createdAt: { | |
type: Date, | |
default: Date.now(), | |
}, | |
}); | |
const Todo = model("Todo", todoSchema); | |
module.exports = Todo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment