Anokrize Anokrize - 20 days ago 6
Node.js Question

Include NodeJs into Ionic

I built a hybrid application with Ionic.
Currently I am using PHP and MySQL as the backend.
As I informed myself this is pretty old style and the new ways are NodeJS with expressJS and MongoDB.
Unfortunately there are thousands of tutorials on how to build a MEAN app(MongoDB, ExpressJS, AngularJS and NodeJs).
This doesn't help me a lot because I don't know how to integrate NodeJS into my existing Ionic app.
Can I only hook them up by changing the POST requests to my new NodeJS server instead of those to the PHP scripts?

I saw some cool ways where the routing of NodeJS was directly hooked up with the app.
How is that possible?

Like if I am navigating to www.testapp.com/test for example

app.get('/test', function (req, res) {
console.log('Hello World!')
})


I want to call this for example.
How is this possible in combination with Ionic?

Thanks a lot.

Answer

For that you can use ExpressJS (fast, unopinionated, minimalist web framework for Node.js).

1.) Install Express

$ npm install express --save

2.) Create simple API. Put this in server.js

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

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

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

If you use MySQL (https://www.npmjs.com/package/mysql) then use this

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

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


       var pool = mysql.createPool({
            host: 'localhost',
            user: '',
            password: '',
            database: ''
        }); 

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

  app.get('/partners', function(req, res,next){

  pool.getConnection(function(err, connection) {

        if (err) {
            console.error("An error occurred: " + err);
        }

       var query = "SELECT * FROM partners ORDER BY idpartner DESC";

        var table = ["tableName"];

        query = mysql.format(query,table);

        connection.query(query,function(err,rows){

            if (err) {               
               return next(err);
            } else {
                res.json({
                    success: true,
                    partners : rows
                });
            }
            connection.release();
        });
    });
});

3.) Run API with CMD or Git Bash.

$ node server.js

4.) Test API directly in web browser or Postman (Supercharge your API workflow. Build, test, and document your APIs faster.).

Visit http://localhost:3000/test or http://localhost:3000/partners

5.) Use API in Ionic app

 $scope.getPartners = function() {
        $http.get("http://localhost:3000/partners", { params: { "key1": "value1", "key2": "value2" } })
            .success(function(response) {
                $scope.firstname = response.firstname;
                $scope.lastname = response.lastname;
            })
            .error(function(response) {
                alert("ERROR");
            });
    }