Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:15:30+00:00 2026-06-15T13:15:30+00:00

I’ve seen examples of Restify where all the endpoints are located on the root:

  • 0

I’ve seen examples of Restify where all the endpoints are located on the root: /users, /data, etc. I know it’s possible to implement nesting like so:

server.get('/users/:user/data/:id', returnData);

and the req.params variable will have all of the request parameters. Example:

{ user: '45', id: '80' }

This seems to work fine if my application has few endpoints, but what if I have a deep and branched data structure that I want to expose through a REST API? Something like:

{
  stuff: {
    points: {
      colors: {
        shinyThings: {},
        dullThings: {}
      }
    },
    ships: {
      enterprises: {},
      starDestroyers: {}
    }
  },
  things: {},
}

Having to write the paths to all of these endpoints by hand just doesn’t seem right. I end up with lots of path definitions and stuff like this:

server.put('/stuff/:stuff/points/:points/colors/:colors/shinyThings/:shinyThings', returnShinyThing);

Is there an easier way to do this with Restify?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:15:31+00:00Added an answer on June 15, 2026 at 1:15 pm

    I’ve come up with a way to do it, although I’m sure there are better alternatives:

    1) Create modules to handle certain actions on endpoints. These modules will be required into a central router module. Example stuff.js:

    exports.list = function(req, res, next) {
      // Code to handle a GET request to /stuff
    };
    
    exports.create = function(req, res, next) {
      // Code to handle a POST request to /stuff
    };
    
    exports.show = function(req, res, next) {
      // Code to handle a GET request to /stuff/:id
    };
    
    exports.update = function(req, res, next) {
      // Code to handle a PUT request to /stuff/:id
    };
    
    exports.destroy = function(req, res, next) {
      // Code to handle a DELETE request to /stuff/:id
    };
    

    2) In the router module define a mapping of actions -> http verbs:

    var actions = {
      list: 'get',
      create: 'post',
      show: 'get',
      update: 'put',
      destroy: 'del'
    }
    

    3) Create an object representing the data structure like this:

    var schema = {
      stuff: {
        _actions: require('./stuff'),
        points: {
          _actions: require('./points'),
          colors: {
            _actions: require('./colors'),
            shinyThings: {_actions: require('./shinyThings')},
            dullThings: {_actions: require('./dullThings')}
          }
        },
        ships: {
          _actions: require('./ships'),
          enterprises: {_actions: require('./enterprises')},
          starDestroyers: {_actions: require('./starDestroyers')}
        }
      },
      things: {_actions: require('./things')},
    }
    

    4) During the router initialization the application passes it a Restify server object to attach the routes to. During initialization a recursive function walks the schema object and when a _actions key is found it calls a second function that attaches the route handlers at the given path to the given server object:

    (function addPathHandlers(object, path) {
      for (var key in object) {
        if (key === '_actions') addActions(object, path);
        else if (typeof object[key] === 'object') {
          var single = en.singularize(path.split('/').pop());
    
          if (path.charAt(path.length - 1) !== '/') {
            path += ['/:', single, '_id/'].join('');
          }
    
          addPathHandlers(object[key], path + key);
        }
      }
    })(schema, '/');
    
    function addActions(object, path) {
      // Actions that require a specific resource id
      var individualActions = ['show', 'update', 'destroy']; 
    
      for (var action in object._actions) {
        var verb = actions[action];
    
        if (verb) {
          var reqPath = path;
          if (individualActions.indexOf(action) !== -1) reqPath += '/:id';
    
          server[verb](reqPath, object._actions[action]);
        }
      }
    }
    

    Notes: This makes use of the lingo module (i.e. the en.singularize() function). It’s also a bit simplified since I removed non-critical parts of the functions, but it should be fully functional as it is.

    The inspiration for this came after looking at the way express-resource does it, although it’s not as refined and simple to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.