我已经在《 Sitepoint Full Stack Javascript with 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);
});
}
}
employee.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
}
}
]
希望能对您有所帮助。
您可以尝试使用.filter方法而不是.find方法。或者将数据库中的数组更改为json。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句