简体   繁体   English

如何在Ionic移动应用程序中运行Node.js服务器?

[英]How to run Node.js server in Ionic mobile app?

I am making an app using MEAN and ionic framework where nodejs is a middleware to connect to the database(mongoDb). 我正在使用MEAN和离子框架制作应用程序,其中nodejs是连接数据库的中间件(mongoDb)。 I need to run the nodejs server using node server.js and the app using ionic serve . 我需要使用node server.js运行nodejs服务器,使用ionic serve运行app。 This is my server.js. 这是我的server.js。

var express          = require('express'),
app              = express(),
bodyParser       = require('body-parser'),
mongoose         = require('mongoose'),
CohortController =require('./www/server/controller/CohortController');

mongoose.connect('mongodb://localhost:27017/persistent');

app.use(bodyParser());

app.get('/api/cohorts',CohortController.list);
app.post('/api/cohorts',CohortController.create);

app.listen(3000,function(){
console.log('Listening...');
})

Now this is my app.js. 现在这是我的app.js. I use http://localhost:3000 to get the JSON. 我使用http://localhost:3000来获取JSON。

 app.controller('CohortController',['$scope','$resource', function($scope,$resource){ var Cohort=$resource('http://localhost:3000/api/cohorts'); Cohort.query(function(results){ $scope.cohorts=results; }); $scope.cohorts=[]; $scope.createCohort= function () { var cohort=new Cohort(); cohort.name=$scope.CohortName; cohort.id=$scope.CohortId; cohort.$save(function(result){ $scope.cohorts.push(result); $scope.CohortName=''; $scope.CohortId=''; }); } }]); 

How can I run the node server when I convert it into a mobile application? 将转换为移动应用程序时,如何运行节点服务器? How the application will use the API? 应用程序将如何使用API​​?

You will have to have your Node.js app running on a server which you would then access (from your Ionic app) via it's public IP. 您必须在服务器上运行Node.js应用程序,然后通过它的公共IP访问(从您的Ionic应用程序)。 So, you wouldn't use http://localhost:3000 to get the JSON, instead you would use something like http://123.456.789.123:3000 . 因此,您不会使用http://localhost:3000来获取JSON,而是使用类似http://123.456.789.123:3000

But, usually, this is not the way you would do it (with the port 3000). 但是,通常情况下,这不是您的方式(使用端口3000)。 What you would additionally do is put (for example) Nginx in front of your Node.js app ( see an example here ) in order to serve your api from the standard HTTP port (80). 你还要做的是将(例如)Nginx放在你的Node.js应用程序前面( 参见这里的例子 ),以便从标准HTTP端口(80)提供api。

So, basically, you can't actually "run Node.js server in Ionic app" - the way you do it is run the Node.js app separate from Ionic and expose its functionality via a standardized API (usually these days REST is what you would want to achieve) which you then "consume" via Ionic's (well, to be exact, it's actually Angular's) $resource module . 所以,基本上,你实际上不能“在Ionic应用程序中运行Node.js服务器” - 你这样做的方式是运行与Ionic分开的Node.js应用程序并通过标准化API公开其功能(通常这些天REST是什么你想要实现的,然后你通过Ionic(嗯,确切地说,它实际上是Angular的) 资源模块 “消耗”。

Hope this helps clear things up a bit. 希望这有助于清理一些事情。

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

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