简体   繁体   English

如何使用Angular app.js文件运行NodeJS API?

[英]How to run NodeJS API with Angular app.js file?

I'm following a few tutorials I found online for creating a web app that uses a RESTful API with the MEAN stack. 我将跟踪一些我在网上找到的教程,以创建一个将RESTful API与MEAN堆栈结合使用的Web应用程序。

I'm having trouble implementing both the API server and the Angular routes together. 我在同时实现API服务器和Angular路由时遇到了麻烦。 I've created a server.js file that handles routes to /api/ , so for example I have: 我创建了一个server.js文件来处理到/api/路由,例如,我有:

...
app.get('/api', function(req, res) {
    res.status(200).json({"message": "Welcome to the app"});
});

So when the API server gets a request it just sends back a JSON message. 因此,当API服务器收到请求时,它只会发送回JSON消息。 Now I also have a public folder with a app.js file that has the Angular code in it: 现在,我还有一个公用文件夹,其中有一个包含Angular代码的app.js文件:

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

app.config(function($routeProvider) {
    $routeProvider
    .when('/', {
        templateUrl: 'views/index.html',
        controller: 'AppController',
        resolve: {
            message: function(Message) {
                return Message.getMessage();
            }
        }
    })
    .service("Message", function($http) {
        this.getMessage = function() {
            return $http.get('/api')
            .then(function(response) {
                return response;
            }, function(response) {
                alert("Error retrieving message");
            });
        }
    })
    .controller("AppController", function(message, $scope) {
        $scope.message = message.data;
    });
});

So when I start my server from the command line running node server.js and go to localhost:5000 (or whatever port it is), I get a Cannot GET / 因此,当我从运行node server.js的命令行启动服务器并转到localhost:5000(或它的任何端口)时,出现Cannot GET /

I'm assuming this is because I don't have a route in my server.js file to '/' . 我假设这是因为我的server.js文件中没有到'/'的路由。

How do I run the app off the app.js file first and have it use the API? 如何先从app.js文件运行该应用程序,并使其使用API​​?

You cannot GET / because the app has not been mounted to be served on your server. 您无法GET /,因为尚未挂载该应用程序以在您的服务器上提供服务。 To do this, you need to create an entry point which will be server from the server, which in turn will handle all your angular routes, views, controllers, etc. 为此,您需要创建一个入口点,该入口点将是服务器中的服务器,服务器将依次处理所有角度路线,视图,控制器等。
In your server.js file, for every request that does not match any of those api routes you defined, you need to forward it to your angular app, in which ng-route will load the view corresponding to the URL you requested. 在server.js文件中,对于每个与您定义的任何api路由都不匹配的请求,您需要将其转发到您的angular应用程序,其中ng-route将加载与您请求的URL相对应的视图。
To mount: 挂载:

app.get('*', function(request, response){
  response.sendfile('/path/to/your/entry.html');
});

Now this entry.html will contain your angular app (or any front end framework you use for that matter) 现在,这个entry.html将包含您的角度应用程序(或您用于此事项的任何前端框架)

You don't need to create route for index.html file just add below line to server file and place index.html file into client folder. 您无需为index.html文件创建路由,只需将以下行添加到服务器文件并将index.html文件放入客户端文件夹即可。

app.use(express.static(path.join(__dirname, '<client_folder_name>')));

When you start your server and open http://localhost:5000 then index.html file automatically render. 当您启动服务器并打开http:// localhost:5000时, index.html文件将自动呈现。

You can do it with the following 4 steps: 您可以按照以下4个步骤进行操作:

1- create your index.html file and put apis in there: 1-创建您的index.html文件并将api放在其中:

app.get('/api', function(req, res) {
    res.status(200).json({"message": "Welcome to the app"});
});

2- require newly created index.html file in server.js: 2-在server.js中需要新创建的index.html文件:

var routes = require('./index');

3- create server and listen to a port: 3-创建服务器并监听端口:

var server = app.listen(5000, function () {
    console.log('nodejs server is listening on port 5000!');
});

4- use index.html file in server.js: 4-在server.js中使用index.html文件:

app.use('/',routes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM