I have a web application, developed with Node.js, AngularJS, Mongoose, Express. Locally, it is working fine. I am making it a mobile web app using Cordova, so after doing cordova platform add XX where XX does not matter if it is browser or iOS or whatever and then cordova run XX, I can see the app loaded (I see the login page), but when I click on the button to login, it makes the call to my API endpoint defined in my server.js but it fails. It fails because it is not invoking the right URL. If I try it with iOS it does file:///api/login, and if I try it with browser is doinglocalhost:8000/api/login.
If I test just without using Cordova, it works fine. To do that I have first to run node server.js and the API calls are done to localhost:5000/api/login.
Any idea why is this happening?
gulp serve and on the other hand I run node app.js.app.js is has the API endpoints and is where I connect to Mongoose:
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
app.use(express.static(__dirname));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(methodOverride());
mongoose.connect('mongodb://localhost/mydb'); // connect to mongoDB database on modulus.io
app.get('/', function(req, res){
res.redirect('/index.html');
});
app.all("/api/*", function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
return next();
});
app.all("/api/*", function(req, res, next) {
if (req.method.toLowerCase() !== "options") {
return next();
}
return res.send(204);
});
/* USERS API */
app.get('/api/users', function(req, res) {
// use mongoose to get all todos in the database
User.find(function(err, users) {
// if there is an error retrieving, send the error. nothing after
res.send(err) will execute
if (err)
res.send(err)
res.json(users); // return all todos in JSON format
});
});
...
/* END PORTFOLIO API */
app.listen(process.env.PORT || 5000)So, when I want to run my webapp using Cordova I do cordova platform add browser and then cordova emulate browser. I can load my app I see it correctly but I cannot have access to the endpoints. The URL that they are linked is wrong so I am getting a 404 error. For instance, taking the case of 'api/users', when I run it locally I know that I'm using port 5000 and I can see that the URL looks like http://localhost:5000/api/users, but when I am in cordova emulate browser the URL turns to be http://localhost:8000/api/users.
It seems to me that I am not running anywhere my app.js file...
...