2017-10-16 176 views
0

這個節點JS打印信息是從另一個問題,我剛纔問Node.js Printing info from JSON file using a function from another JS file從JSON文件中使用函數從另一個文件V2.0

在我剛纔的問題我有問題,呼籲從我的數據服務功能的延續.js文件打印我的JSON數組中的所有項目,並且解決了它,但現在我正在努力做類似的工作,只打印通過url指定的JSON數組中的員工。例如http://localhost:8080/employeesstatus=5將只打印與僱員是越來越印刷

SERVER.JS

var HTTP_PORT = process.env.PORT || 8080; 
var express = require('express'); 
var data = require('./data-service'); 
var fs = require('fs'); 
var app = express(); 
var object = require('./data-service'); 

console.log("Express http server listening on 8080"); 

//app.get('/employees', function(req,res){ 
// return object.getAllEmployees() 
// .then((response) => res.send(response)) 
//}); //QUESION FROM PREVIOUS POST WHICH WAS RESOLVED 


app.get('/employees:?status=:value', function(req,res){ 
return object.getEmployeesByStatus(req.params.value) 
    .then((response) => res.send(response)); 

}); 

DATA SERVICE.JS

var employees = []; 
var departments = []; 
var error = 0; 
var fs = require("fs"); 


function initialize(){ 

employees = fs.readFileSync("./data/employees.json", 'utf8', function(err, data){ 
    if(err){ 
     error = 1; 
    } 
    employees = JSON.parse(data); 

}); 


departments = fs.readFileSync("./data/department.json", 'utf8', 
function(err, data){ 
     if(err){ 
      error = 1; 
     } 
     departments = JSON.parse(data); 

    }); 
} 
function check() { 
    return new Promise(function(resolve,reject){ 

     if (error === 0){ 
      resolve("Success"); 

     } 
     else if(error === 1){ 
     reject("unable to read file"); 
     } 
    })  
}; 

var getAllEmployees = function(){ 

    return check().then(function(x){ 
    console.log(x); 
    console.log(employees); 
    return employees; 

    }).catch(function(x){ 
    console.log("No results returned"); 
    }); 
} 

var getEmployeesByStatus = function (status){ 
return check().then(function(x){ 
var employees2 = JSON.parse(employees); 
for (var i = 0; i<employees2.length; i++){ 
    if (employees2[i].status == status){ 
     return console.log(employees2[i]); 
     } 
    }  
    }).catch(function(){ 
     console.log("no results returned"); 
    }) 
} 

module.exports.getAllEmployees = getAllEmployees; 
module.exports.getEmployeesByStatus = getEmployeesByStatus; 

的2個功能中的5然而沒有一個狀態問題

app.get('/employees:?status=:value', function(req,res){ 
return object.getEmployeesByStatus(req.params.value) 
    .then((response) => res.send(response)); 

}); 

var getEmployeesByStatus = function (status){ 
return check().then(function(x){ 
var employees2 = JSON.parse(employees); 
for (var i = 0; i<employees2.length; i++){ 
    if (employees2[i].status == status){ 
     return employees2[i]; 
    } 
}  
}).catch(function(){ 
    console.log("no results returned"); 
}) 
} 

回答

0

1)您應替換爲以下

app.get('/employees/status=:value', function(req,res){ 
return object.getEmployeesByStatus(req.params.value) 
.then((response) => res.send(response)); 
}); 

/員工路線您可以使用http://localhost:8080/employees/status=5

2)返回EMPLOYEES2 [I],而不是執行console.log訪問(EMPLOYEES2 [1])。

+0

現在,訪問http:// localhost:8080/employees?status = 5端點。它應該返回員工狀態5. –

+0

之後的任何事情?將被構造爲查詢對象。如果你想爲path param添加狀態,我們應該以不同的方式來完成。我現在會更新。 –

+0

是的,我試圖使用app.get('/ employees:?status =:value',function(req,res){...功能 – Workkkkk

相關問題