我目前正在运行一个带有angular js应用程序的express服务器express.js。我使用UI路由器https://github.com/angular-ui/ui-router带有stateprovider和state.go.
我有一个要求允许在浏览器中键入url
/getEmployeeDetails/1234
我是不是走对了路,因为下面的代码可以添加到express server.js中来实现这一点,或者我应该在angular应用程序状态中处理这一点。
app.get('/p/:empId', function(req, res) {
controller.getEmpDetaails(req.params.empId);
state.go("employeeDetailsview")
});
我不确定在Express服务器中编写角度代码的原因是什么,但您应该真正将客户端代码与服务器代码分开。
我想你正试图从服务器上通过ID获取一些员工的详细信息。通常的方法是从客户端向服务器发送一个带有ID号的HTTP请求。然后,服务器将处理HTTP请求(可能从数据库中获取一些数据),并向客户端返回HTTP响应。然后客户端将处理响应并对其进行处理
在你的客户中,你可以做这样的事情:
$http.post('SERVER_URL/getEmployeeDetails/', {'id': 1234})
.then(function(response){
// Response.data will have the data returned from the server
// Do something with it. for example, go to other state and pass the data to it
state.go("employeeDetailsview", {'employee': response.data});
});
以上内容将请求id为1234的员工并用它做些什么。
在服务器端:
app.post('getEmployeeDetails/', function(req, res) {
var employeeId = req.body.id; // YOU SHOULD USE THE BODY-PARSER MODULE IN ORDER FOR THIS TO WORK.
....
// Do something with ID
....
// Return some data to the client - for example an employee object
res.status(200).json({'data': employeeObject});
});