我在一本名为"Sitepoint MEAN“的书中学习了一个教程,我刚刚完成了第6章,应该创建一个带有”数据库“的”服务器“。数据库不过是一个JSON文档。然而,尽管(我所看到的),我的代码是他的直接副本,我得到了在标题中提到的错误,当我试图运行它。造成这个问题的原因是var result = data.find(function(item) {... (位于employees.js中的线路,大约是第16行)。我看不出我还能做些什么,希望你们能找到解决我问题的办法。
我有几个不同的文件用来做这个。
Index.js:
var http = require('http');
var employeeService = require('./lib/employees');
var responder = require('./lib/responseGenerator');
var staticFile = responder.staticFile('/public');
http.createServer(function(req,res) {
// a parsed url to work with in case there are parameters
var _url;
//In case the client uses lower case for methods
req.method = req.method.toUpperCase();
console.log(req.method + ' ' + req.url);
if (req.method !== 'GET') {
res.writeHead(501, {
'Content-Type': 'text/plain'
});
return res.end(req.method + ' is not implemented by this server.');
}
if (_url = /^\/employees$/i.exec(req.url)) {
//return a list of employess
employeeService.getEmployees(function(error, data){
if(error) {
return responder.send500(error, res);
}
return responder.sendJson(data,res);
});
} else if (_url = /^\/employees\/(\d+)$/i.exec(req.url)){
//find the employee by the id in the route
employeeService.getEmployee(_url[1], function(error, data) {
if (error) {
return responder.send500(error, res);
}
if(!data) {
return responder.send404(res);
}
return responder.sendJson(data,res);
});
} else{
res.writeHead(200);
res.end("static file")
}
}).listen(1337);
console.log('server running');employee.js
var employeeDb = require('../database/employees.json')
exports.getEmployees = getEmployees;
exports.getEmployee = getEmployee;
function getEmployees (callback) {
setTimeout(function() {
callback(null, employeeDb);
}, 500);
}
function getEmployee (employeeId, callback) {
getEmployees(function (error, data) {
if (error) {
return callback(error);
}
var result = data.find(function(item) {
return item.id === employeeId;
});
callback(null, result)
});
}responseGenerator.js
var fs = require('fs');
exports.send404 = function (reponse) {
console.error('Resource not found');
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.end('Not Found');
}
exports.sendJson = function(data, response) {
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.end(JSON.stringify(data));
}
exports.send500 = function(data, response) {
console.error(data.red);
reponse.writeHead(500, {
'Content-Type': 'text/plain'
});
response.end(data);
}
exports.staticFile = function(staticPath) {
return function(data, response) {
var readStream;
// Fix so routes to /home and /home.html both work
data = data.replace(/^(\/home)(.html)?$/i,'$1.html');
data = '.' + staticPath + data;
fs.stat(data, function(error, stats) {
if (error || stats.isDirectory()) {
return exports.send404(response);
}
readstream = fs.createReadStream(data);
return readStream.pipe(response);
});
}
}Employees.json(“数据库”)
[
{
"id": "103",
"name": {
"first": "Colin",
"last": "Ihrig"
},
"address": {
"lines": ["11 Wall Street"],
"city": "New York",
"state": "NY",
"zip": 10118
}
},
{
"id": "104",
"name": {
"first": "Idiot",
"last": "Fjols"
},
"address": {
"lines": ["Total taber"],
"city": "Feeeee",
"state": "Whatever",
"zip": 10112
}
}
]希望你能帮上忙。
发布于 2015-03-19 21:22:50
您可以尝试使用.filter方法而不是.find方法。或者将数据库中的数组更改为json。
发布于 2015-04-11 23:21:45
该书第80页:
查找方法--查找方法的简要说明。它在ECMAScript 6的规范中,但目前在Node运行时(NodeVersion0.10.32)中不可用。对于本例,您可以为Array.find方法添加一个多边形填充。多填充是一个术语,用来描述在尚未支持它的环境中支持未来JavaScript特性的代码。您还可以在lib/employees中编写一个额外的方法,该方法基于ID在数组中定位元素。
如果您没有加载图书源代码,您可以看到作者为使find方法可用而做的工作:
Array.prototype.find = function (predicate) {
for (var i = 0, value; i < this.length; i++) {
value = this[i];
if (predicate.call(this, value))
return value;
}
return undefined;
}发布于 2015-12-24 22:19:52
在index.js (最后一个else块)中,Em指出:
该程序将继续以“静态文件可能”响应每个请求,而不是被前面的路由测试截获。
https://stackoverflow.com/questions/29155084
复制相似问题