【问题标题】:Express Server, calling state.go and running a method in ControllerExpress Server,调用 state.go 并在 Controller 中运行方法
【发布时间】:2016-03-02 19:23:59
【问题描述】:

我目前正在运行带有 Angular js 应用程序的快速服务器 express.js。我将 UI 路由器 https://github.com/angular-ui/ui-router 与 stateprovider 和 state.go 一起使用。

我需要允许在浏览器中输入网址

/getEmployeeDetails/1234

我走的是正确的路线吗,因为可以将以下代码添加到 express server.js 以实现此目的,或者我应该在 Angular 应用程序状态下处理此问题。

app.get('/p/:empId', function(req, res) {
  controller.getEmpDetaails(req.params.empId);
  state.go("employeeDetailsview")
});

【问题讨论】:

  • 你为什么在你的 express 服务器中使用 ui-router?

标签: angularjs node.js angular-ui-router


【解决方案1】:

我不确定在 Express 服务器中编写 Angular 代码的原因是什么,但您确实应该将客户端代码与服务器代码分开。

我假设您正在尝试通过 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});

});

【讨论】:

  • 感谢 VanderVidi,但它是我正在寻找的部分“// 使用 ID 进行操作”。类似于 stackoverflow.com/questions/13860899/… for stateprovider 的东西
  • 只需在此处实现一些从数据库中获取员工的代码。
猜你喜欢
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多