programm a todo application in typescript in typescript

To program a todo application in TypeScript, you can follow these simple steps:

  1. Set up a TypeScript project using npm init.
  2. Install the necessary dependencies: npm install express body-parser cors mongoose.
  3. Create a Todo model using Mongoose.
index.ts
import * as mongoose from 'mongoose';

export interface ITodo extends mongoose.Document {
    text: string;
    completed: boolean;
}

const TodoSchema = new mongoose.Schema({
    text: { type: String, required: true },
    completed: { type: Boolean, default: false },
});

export default mongoose.model<ITodo>('Todo', TodoSchema);
333 chars
14 lines
  1. Create an Express server and define routes for creating, reading, updating, and deleting todos.
index.ts
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { default as Todo, ITodo } from './models/todo';

const app = express();
const router = express.Router();
const port = 3000;

app.use(cors());
app.use(bodyParser.json());

router.get('/', (req, res) => {
    res.send('Hello World!');
});

router.get('/todos', (req, res) => {
    Todo.find((err, todos) => {
        if (err) {
            console.log(err);
        } else {
            res.json(todos);
        }
    });
});

router.get('/todos/:id', (req, res) => {
    Todo.findById(req.params.id, (err, todo) => {
        if (err) {
            console.log(err);
        } else {
            res.json(todo);
        }
    });
});

router.post('/todos', (req, res) => {
    const todo = new Todo({
        text: req.body.text,
    });
    todo.save((err) => {
        if (err) {
            console.log(err);
        } else {
            res.json(todo);
        }
    });
});

router.put('/todos/:id', (req, res) => {
    Todo.findByIdAndUpdate(req.params.id, {
        $set: {
            completed: req.body.completed,
        },
    }, (err, todo) => {
        if (err) {
            console.log(err);
        } else {
            res.json(todo);
        }
    });
});

router.delete('/todos/:id', (req, res) => {
    Todo.findByIdAndRemove(req.params.id, (err, todo) => {
        if (err) {
            console.log(err);
        } else {
            res.json(todo);
        }
    });
});

app.use('/', router);

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

mongoose.connect('mongodb://localhost:27017/todos', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
}, (err) => {
    if (err) {
        console.log(err);
    } else {
        console.log('Connected to MongoDB');
    }
});
1834 chars
90 lines
  1. Test your application using npm start and sending requests to the endpoints defined in your Express server.
curl http://localhost:3000/todos
curl -X POST http://localhost:3000/todos -d '{"text": "Buy groceries"}' -H 'Content-Type: application/json'
curl -X PUT http://localhost:3000/todos/<id> -d '{"completed": true}' -H 'Content-Type: application/json'
curl -X DELETE http://localhost:3000/todos/<id>
295 chars
5 lines

gistlibby LogSnag