C9 not returning my api call information in browser but it does in terminal


#1

Hey guys,

I’m trying to access my api information in my angular page but it says 404 found.

My angular code is:

    app.controller("dataCtrl", function($scope, $http) {
  $http.get('/api/jobs').then(function (resp) {
    console.log(resp.data);
    $scope.data = resp.data;
  }, function(error,resp) {
    console.log(error, resp);
  });
});

My mysql.js is

app.get('/api/jobs', function(req, res, next) {
  connection.query('SELECT * FROM jobs ORDER BY id ASC', function(err, results) {
      if (err) {
          console.log('There was a problem reading from jobs database :' + err.stack);
          res.sendStatus(500).send('There was a problem reading from jobs database');
          return;
      }

      console.log('results:', results);
      res.send(results);
  });
});

when I do

curl localhost:3000/api/jobs

in terminal it does return the information but when I do

https://job-lists-gchavez1011.c9users.io/api/jobs

or

https://job-lists-gchavez1011.c9users.io:3000/api/jobs

Nothing is returned, what am I doing wrong?


#2

I’ve tried to diagnose it, but can’t seem to find the problem. Do you have a link to the workspace? Is there any stacktrace on the server or the client?

Thanks and I’ll try to figure it out as soon as I have that! :slight_smile:


#3

Link to my workspace code or project?

https://job-lists-gchavez1011.c9users.io/node_modules/angular-ui-bootstrap/index.html


#4

Looking at your code, I have a few questions: how is the server running? The error page is an Apache error page, but you’re writing NodeJS. So is the mysql.js file being run with “node mysql.js”, or are you just using the Apache runner (which I believe doesn’t support node). Common practice is to write code outside of the node_modules folder and load modules in that code, so I’m wondering which files you created and which ones were already there?


#5

My mysql.js is being ran by node mysql.js.

I created the index.html, the index.js for it, and style.css, and mysql.js


#6

Then the only reason I could imagine is that Apache is handling the request before Node, so it sends an error page and stops any other code running. Could you possibly try running just the node server by itself?


#7

Silly quesiton…how would I do that?


#8

There are no silly questions. I would suggest closing all Apache runners you have open, and then checking if the error still happens:

Should look something like this, if the Run button says Stop, press it.
Then make sure mysql.js is running using node mysql.js, and go to api/jobs.