Search code examples
angularjsnode.jsexpressangular-resource

AngularJS $resource GET params appear in URL


My REST backend [ based on NodeJS/express/mongojs] is complaining 404 (not found) when Params are attached as part of URL. Backend rest interface is coded as below;

var express = require('express');
var router = express.Router();
router.get('/login', auth.signin); //auth.signin is code to verify user

Above REST service is consumed by AngularJS based frontend through $resource as below;

Definition:

angular.module('myapp').factory('signinmgr', function($resource) {
    return $resource("http://localhost:3000/login", {}, {
        'get': {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            }
        }});

Usage:

signinmgr.get({'username':'myname', 'password':'mypass'}, function(data){
        //success 
    }, function(x){
        //failed
    });

Problem:

  1. Frontend code above produces a URL to consume REST service where parameters are part of URL i.e. http://localhost:port/login?username=myname&password=mypass [if I use GET method, POST is OK]. I wanted my front end to keep URL as http://localhost:port/login and post any parameters through body as backend is using req.body.paramName to read those. [Actual Solution]

  2. If (1) cannot be done, and my frontend is sending params as part of URL, I needed help as to know how to equip my backend to allow this URL with parameters so that backend doesnt return 404 as the base URL http://localhost:port/login is already there.

PS: for (1), I tried this thread with data:{username:'',password:''} but of no use. Please help if I am missing something very obvious or some concept.


Solution

  • Each request that my nodejs/expressjs backend receives has three places for passed attributes;

    params{}
    query{}
    body{}
    

    My problem (1) cannot be fixed in case I want to use GET method since with GET request parameters are visible as part of URL i.e. http://localhost:port/login?username=myname&password=mypass. To send my username/password I had to use POST that sends parameters as part of body{}.

    My problem (2) was that I was using GET and mistakenly looking for parameters in body{} of request. Instead, parameters passed as part of URL in GET request are added to query{} of the request.