1

I recently started programming on nodeJs.

I am using Angular JS, resource to call API's as

demoApp.factory('class', function ($resource) {
      return $resource('/class/:classId', { classId: '@_classId' }, {
        update: { method: 'PUT' }
      });
    });

And in Controller, I have delete method as;

   // The class object, e {classId: 1, className: "Pro"}

$scope.deleteClass = function (class) {
        var deleteObj = new Class();
        deleteObj.classId = class.classId;
        deleteObj.$delete({classId : deleteObj.classId}, function() {
            growl.success("Class deleted successfully.");
            $location.path('/'); 
            },function () {
                growl.error("Error while deleting Class.");
            }
        ); 
    };

Using browser, I verified call goes to :

http://localhost:3000/class/1

Now in node Js, How should I extract value from Url,

In server.js

app.use('/class', classController.getApi);

In classController.js

exports.getApi = function(req, resp){
    switch(req.method) {
        case 'DELETE':
            if (req) {
                // how to extract 1 from url.
            }
            else {
                httpMsgs.show404(req, resp);
            }
            break;        

I have tried ,

console.log(req.params);
console.log(req.query);

But no luck.

I am seeing

    console.log(req._parsedUrl);
query: null,
pathname: '/class/1',
path: '/class/1',

Any help appreciated.

Sawan Kumar
  • 327
  • 1
  • 5
  • 13

3 Answers3

1

This should be a get call right ? You can use angular $http service, with method as get. Replace your app.use('/class') with app.get('/class', function). Then you can use req.param('classId') to retrieve data. I think it should work.

Try updating your app.use to app.use('/class/:classId'), then try req.params.classId

Aravind
  • 2,290
  • 1
  • 11
  • 21
0

Try using req.hostname as in:

`http://host/path'

eschie
  • 194
  • 1
  • 12
0

Check this answer.

Tldr;

var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;

Also read the docs on node url.

Community
  • 1
  • 1
amu
  • 778
  • 6
  • 16
  • As I said, it does not have query or param, request is as localhost:3000/class/1. So query or param function does not work. – Sawan Kumar Jun 15 '16 at 07:28