0

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.

2 Answers 2

1

Try the $http service instead:

angular.module('myapp').factor('signinmgr', function($http) {
    return {
        login: function (username, password) {
            $http.post("http://localhost:3000/login", {
                username: username,
                password: password
            }
        }
    };
});

signinmgr.login('myname', 'mypass').then(function(data){
    //success 
}, function(x){
    //failed
});
Sign up to request clarification or add additional context in comments.

1 Comment

I selected $resource over $http for some reasons that made sense a couple months back and it was working perfectl. My backend at that time was based on restify. I changed my backend to express in current implementation and it starts complaining with GET. Any idea on what could be wrong with current implelementation?
1

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.

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.