Bolboa Bolboa - 11 months ago 65
Node.js Question

How to call node server using ionic

I'm using ionic with

for the front-end. I also setup a separate folder for my server side component. This is my

var express = require('express');
var app = express();

app.get('/', function (req, res) {
res.send('Hello World!');

app.listen(3000, function () {
console.log('Example app listening on port 3000!');

As you can see it is a simple Hellow World example on my localhost.

I am going to use
to make my server file public. My question is, after I do that, how can I call the
server file from ionic?

Do I call it from my angular module? If so, how can I do that? Can someone tell me what code is needed to add in ionic so I can connect to the server file? I am trying to use node js as my backend but having difficulty.

Answer Source

ngrok will allow you to access your localserver from outside your network.

Here is a basic example of how you call your server. Depends where you need to call it but this is how I do it using ngResource

angular.module('', ['ngResource'])

.factory('Session', function ($resource) {
  return $resource('http://XXX.XXX.XXX.XXX:5000/sessions/:sessionId');

If you run your app from a browser with ionic server you can use http://localhost:5000.

If you use your phone and if it is on the same network than your server you can use your private IP, something like http://192.168.XXX.XXX

And if you want to try it from another network ngrok is a good choice.