如何使用json获取表达js数据

时间:2015-01-26 13:13:38

标签: json node.js express

当我转到localhost:3000 / todos以及转到localhost:3000 / todos / 1或localhost:3000 / todos / 2时的具体数据时,如何更改以获取所有数据?

我的app.js是:

/*jslint node:true*/
'use strict';


var express = require('express');
var app = express();

function Todo() {
this.todos = [];
this.nextId = 1;
}

Todo.prototype.find = function (id) {
var todo = this.todos.filter(function (item) {
    return item.taskId === id;
})[0];
if (null === todo) {
    throw new Error('task not found');
}
return todo;
};

app.get('/todos/:id', function (request, response) {
var taskId = request.params.id;
try {
    response.json(Todo.find(taskId));
} catch (exeception) {
    response.sendStatus(404);
}
});
app.listen(3000);

我的todos.json是:

[
{
"id": 1,
"name": "premiere action"
},
{
"id" : 2,
"name": "deuxieme action"
}
]

提前谢谢

0 个答案:

没有答案