0

I'm trying to pass multiple parameters in a URL with no luck. I'm not sure if it makes a difference but I am doing it through Angular. I'm trying to send the request to a REST API backend that I know works for single requests. Here is what my backend looks like

index.js

var express = require('express');
var router = express.Router();
var game = require('./game');
router.get('/api/v1/gameRefined/:from_datepicker:to_datepicker:from_timepicker:to_timepicker:selectLevel', game.getAllRefined);
module.exports = router;

game.js

...dbconnection stuff...
var game={
getAllRefined: function(req, res) {
    var refines = req.params;
    console.log("getting refined games");
    pool.getConnection(function(err, connection){
    var query = connection.query('SELECT * FROM game WHERE date >= ? AND date <= ? AND time >= ? AND time <= ? and level = ?', [refines.from_datepicker, refines.to_datepicker, refines.from_timepicker, refines.to_timepicker, refines.selectLevel], function(err, rows) {
      connection.release();
if(err) {
  throw err;
}else{
  res.json(rows);
    }
  });
})
  },
}
module.exports = game;

I send the request from this factory

.factory('gameFactory', ['$http',
function($http) {
var _gameFactory = {};
_gameFactory.getRefinedGames = function(dateFrom,dateTo,timeFrom,timeTo,level) {
  var encodedParam = encodeURIComponent(baseUrl + '/api/v1/gameRefined/?from_datepicker='+dateFrom+'&to_datepicker='+dateTo+'&from_timepicker='+timeFrom+'&to_timepicker='+timeTo+'&selectLevel='+level+'');
  return $http.get(encodedParam);
}

return _gameFactory;
}])

That sends this request that comes back as a 404: http://localhost:8100/http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fv1%2FgameRefined%2F%3Ffrom_datepicker%3D2015-02-05%26to_datepicker%3D2015-02-19%26from_timepicker%3D12%3A00%26to_timepicker%3D18%3A00%26selectLevel%3D1

I have tried it encoded and not encoded, with forward slashs, with semi colons, but nothing has worked so far. I don't know why a localhost gets appended at the start, but even trying it in postman without the first localhost still is a 404 error.

How should I be doing this? Any help is appreciated. Thank you.

3
  • Trying to parse through your issue(s). 1) You are not posting a valid URL? 2) Express is unable to read your URL? Is that right - both, one, neither? Commented Feb 12, 2015 at 19:10
  • I think express can't read the URL I am sending, but I think that is because I am sending it in the wrong format. So...both unfortunately! Commented Feb 12, 2015 at 19:15
  • 1
    Yeah it's the wrong format. I always setup my routes like... /api/v1/:param/:param/:param... with each param having its own slash. And the request would just be the same... '/api/v1/' + param1 + '/' + param2 etc.... You dont need to do the whole ?= etc Commented Feb 12, 2015 at 19:22

1 Answer 1

1

First, other than separating the work into a factory, you aren't really using Angular here. Second, I would consider posting to your API, return JSON results from the API, and then use a promise to read the data back into Angular.

  1. Use $http.post

Let Angular do this work for you. Something like the below will generate the URL request for you. The return value of $http is also a promise, so using .success and .error will allow you to parse any returned data as well, even if it is just a success or failure message - but it is a great method of passing data between server/API and client.

.factory('gameFactory', ['$http',
  function($http) {
    return {
      reachAPI: function(dateFrom, dateTo) {
        $http.post('http://localhost:8080/api/v1', {
          'dateFrom': dateFrom,
          'dateTo': dateTo
        })
        .success(function(data, status, headers, config) {
          *things happen*
          data.somethingReturned // from your API
          });
        }
}]);
  1. Consider body-parser

I know you said you have confidence in your REST API structure, but body-parser is an Express middleware that can parse URL-encoded strings and may prove helpful in reading your data. Personally, I think it lends towards more readable code.

var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
router.route('/api').post(function(req, res) {
  *things happen*
  req.body.dateFrom //find your data
  *things happen*
  res.json(returnedData);
});

Hope that helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.