首页 > 解决方案 > 使用 node.js 和 express 列出待办事项

问题描述

我已经尝试解决这个问题一个星期,但无济于事,这是我的期末考试,必须在明天之前通过。整个学期我们学习的主要是适用于浏览器的 JS。而在上两节课中,Node.js + Express 向我们进行了肤浅的解释,这一次还不足以弄清楚。如果您理解,我将非常感谢您的帮助,因为这是一项非常重要的测试,我的最终分数取决于它,提前谢谢您!

对于这部分,我需要

无法添加 TODO 或 COMPLETED 上的任何重复任务,并弹出警报

当浏览器关闭、刷新或从本地存储中清除时,数据仍然存在

当任务移动到 COMPLETED 时,数据在刷新、关闭和/或清除缓存时保持在同一部分

当从 COMPLETED 中删除任务时,即使刷新、关闭和/或清除缓存,它也不再出现并保持删除状态

我不能使用 LOCAL STORAGE 来存储输入数据

let taskDatastore = {
    "todo": [
        "finish test 2",
    ],
    "completed": [
        "read the test instructions",
    ],
};

// GET handler returns all tasks to the client
app.get('/tasks', (req, res) => {
    res.set('Content-Type', 'application/json');
    /*
        Returns 200 with response body containing the datastore
    */
    res.status(200).send(taskDatastore);
});

// POST handler adds a task to a list
app.post('/task', (req, res) => {
    res.set('Content-Type', 'application/json');
    
const task = req.body.task;

if (taskDatastore.todo.includes(task) || taskDatastore.completed.includes(task)) {
    res.status(400).send({});
} else {
    // send 201 status with no response body
    res.status(201).send({taskDatastore});
}

console.log (req.body.task);
});

    
    /*
        Input: data coming into this handler will be in the form:

        {"task": "mytask"}

        You can see contents of the incoming request with:
        console.log(req.body.task). "req" is the request
        and within it, the property req.body holds the incoming data
        above from the client request, hence "task" is a property of
        that incoming request data object.

        Output: This handler should send the datastore back to
        the client with a 201 response code if successful, and return
        and 400 response code with no response body if the task already
        exists in either the TODO or Completed list.
    */

    /*
        YOUR CODE HERE
    */

    // send 201 status with no response body
    res.status(201).send({})
});

// DELETE handler deletes all tasks in TODO and Completed lists
app.delete('/tasks', (req, res) => {
    /*
        This handler will return 204 status code with an empty
        body when all tasks are deleted from the TODO and Completed
        lists.
    */

    /*
        YOUR CODE HERE
    */

    // send 204 status code with no response body
    res.status(204).send()
});

// DELETE handler deletes a task from the TODO list
// and adds the task to the Completed list
app.delete('/task/todo/:task', (req, res) => {
    /*
        req.params gets a parameter variable
        The :task part of the path indicates
        data is being passed in the URL instead
        of the query or in post data. To get this
        data, you would use `req.params.task`

        This handler will return 204 status code with
        no response body after deleting the task from the
        TODO list and adding the task to the Completed list.
    */

    /*
        YOUR CODE HERE
    */

    // send 204 status code with no response body
    res.status(204).send()
});

// DELETE handler deletes a task the client passes via the URL path
app.delete('/task/completed/:task', (req, res) => {
    /*
        This handler (like the previous handler) will return
        a 204 status code with no response body after it deletes
        the task from the Completed list.
    */

    /*
        YOUR CODE HERE
    */

    // send 204 status code with no response body
    res.status(204).send();
});

// This base handler will serve the HTML page todo.html to localhost:3000 when
// the app is started
app.get('/', (req, res) => {
    res.sendFile('todo.html', { root: __dirname });
});

标签: javascriptnode.jsajaxexpress

解决方案


推荐阅读