ExpressJS & Cordova testing app: The requested URL was not found on this server

453 views
Skip to first unread message

Federico Martín Alconada Verzini

unread,
Apr 27, 2016, 8:29:24 AM4/27/16
to phonegap

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?

Kerri Shotts

unread,
Apr 27, 2016, 2:51:38 PM4/27/16
to phonegap
It seems to me that you're treating PhoneGap more like a server than a browser. PhoneGap itself will not process server code, so things like PHP, Perl, and Node.js aren't going to work. Instead, you do dynamic updates via JavaScript and communicate with your server via XMLHttpRequest or web sockets. 

But perhaps I'm misunderstanding your post. Sharing some code would probably help make it clear.

Federico Martín Alconada Verzini

unread,
Apr 30, 2016, 5:07:11 PM4/30/16
to phonegap
Thanks for your answer. I have a web application, developed with Node.js, AngularJS, Mongoose, Express and I'm usingGulp. If I want to run it locally, I just do 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...

Kerri Shotts

unread,
May 2, 2016, 10:51:30 AM5/2/16
to phonegap
PhoneGap is not a Node server, and does not have access to Node.js stuff like express. So if you point PhoneGap at "app.js" or expect PhoneGap to somehow start "app.js", you're out of luck.

"app.js" would be need to be running on a separate server, and your PhoneGap app would use XMLHttpRequest/Fetch/Web Sockets to connect to that server to send and receive data.

If the latter is what you're doing, then you'd just need to hard-code the server & port in those links that need to talk to the server. Ideally, make it a configurable setting so that you can easily switch between a test and production environment. 

On Saturday, April 30, 2016 at 4:07:11 PM UTC-5, Federico Martín Alconada Verzini wrote:
...
Reply all
Reply to author
Forward
0 new messages