【问题标题】:AngularJS routing paths to Express serverAngularJS 路由到 Express 服务器的路径
【发布时间】:2018-10-10 23:30:18
【问题描述】:

我是 AngularJS、Node 和 Express 的初学者。
我什至连最基本的路由都无法使用 $http.get 或 $http.post。
我在网上找到的每个示例都忽略了文件名、Angular 控制器路径和服务器中的路由路径之间的关系。他们只显示代码而不显示路径和文件名。

这是我的 html 文件:“/programs/static/example.html”:

<!DOCTYPE html>
<html ng-app="exampleApp">
<head>

</head>
<body>
    <div ng-controller="exampleController">
        <input type="submit" value="Click me" ng-click="request()">
    </div> 
    <script src="http://code.angularjs.org/1.6.9/angular.min.js"></script>
    <script src="/js/example-controller.js"></script>
</body>
</html>

这是我的 Angular 控制器:“/programs/static/js/example-controller.js”

var exampleApp = angular.module('exampleApp', []);

exampleApp.controller('exampleController', function($scope) {

    $scope.request = function() {
        data="test";
        $http.post("/" , data);
    };

});

这是我的 NodeJS 服务器:“/programs/node-server.js”

var express = require("express");

var app = express();
app.use('/', express.static('./static'));

app.post('/example.html/', function(req, res){
    console.log("request received.");
});


app.listen(80);

问题是我的服务器的控制台没有显示“收到请求”。这是我构建的一个示例应用程序,只是为了将其发布到 Stack Overflow。在我的真实应用中,我尝试了尽可能多的不同路线。我可以让我的节点服务器中的 app.get() 使用简单的 URL(不是 $http 方法)工作。但从我的测试中很明显,我的 Angular 控制器没有发送任何请求,或者服务器没有接收到它们。

【问题讨论】:

  • 在你的控制器中注入 $http 依赖:exampleApp.controller('exampleController', function($scope, $http)...
  • 如果您真的告诉我们问题出在哪里,这将对我们有很大帮助。什么不工作?什么是错误消息(如果有)。包括 AngularJS(在浏览器开发工具中)和 Express 服务器的错误消息
  • @RicardoTribaldos 我添加了依赖项,但它不起作用。
  • @Pop-A-Stash 问题是我服务器的控制台没有显示“收到请求”。这是我构建的一个示例应用程序,只是为了将其发布到 Stack Overflow。在我的真实应用中,我尝试了尽可能多的不同路线。我可以让我的节点服务器中的 app.get() 使用简单的 URL(不是 $http 方法)工作。但从我的测试中很明显,我的 Angular 控制器没有发送任何请求,或者服务器没有收到它们。
  • 如果您有更多信息,请编辑您的原始问题。不要将其添加到 cmets。评论不像您的问题那样明显,这将有助于其他人帮助您

标签: angularjs node.js express routes


【解决方案1】:

您的 POST 端点的名称中不应包含文件扩展名。 "example.html" 的任何帖子都将尝试返回一个名为 example.html 的实际 html 文件,如果您的 \static 文件夹中有任何此类文件名。您的 API 端点应该有明确的唯一名称,没有文件扩展名:

app.post('/test', function(req, res){
  console.log("request received.");
});

当使用$http.post 并将"/" 作为参数传递时,发布到的实际URL 将与您当前的URL 相关。发布到服务器时,您应该始终使用绝对 URL(除非您知道自己在做什么)。您还应该实现.then 和.catch 方法。这些将帮助您调试问题:

$http.post("http://localhost:8080/test", data).then(function(response) {
  console.log(response);  //this will log a success
}).catch(function(error) {
  console.log(error);  //this will log the error
});

您也忘记在控制器中注入$http 提供程序:

exampleApp.controller('exampleController', function($scope, $http) {
  ...
});

【讨论】:

  • 感谢您的解释。我试过你说的。但是,我确信我的问题与我的端点有关,例如 app.post() 中的“/test”或 $http.post() 中的“localhost/example.html/test”。我试过你说的,有0条消息发布到控制台。没有。没有错误,没有成功消息。我做错了什么,我不知道是什么。
  • 哪个控制台?您需要尽可能具体。这是 Express 控制台,还是 Web 浏览器中的开发工具?
  • 另外,我在原始帖子中包含了我的路径和文件名,因为我知道这就是问题所在。
  • 我相信你应该这样做 {{request()}},这就是你在 Angular 中访问 $scope 的方式
  • 我让它工作了。谢谢@Pop-A-Stash。我将您的答案标记为解决方案。
猜你喜欢
  • 1970-01-01
  • 2015-05-22
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
相关资源
最近更新 更多