Git Product home page Git Product logo

restify-router's People

Contributors

budnieswski avatar jimmywarting avatar otaviotech avatar ukayani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

restify-router's Issues

Error handler is always executed

Does this library support adding error-handling middleware via router.use()? I'm trying to create a generic handler for a router, but it's being executed before the route handlers.

Example:

app.get('/api/data', function dataHandler(req, res, next) {
    // Process request
});


// Always executes before `dataHandler()`. 
app.use(function errorHandler(err, req, res, next) {
    // Handle error
});

Using restify-validations-engine

I want to use this validation engine, however I can't seem to figure out where to add the validate option.

Its supposed to be added like this. Any help will be much appreciated.

server.get({
    url: '/my-url',
    validate: { // Entry point of the module
        params: { // Which scope
            myParam: { // The field to validate
                required: true // A validator
            }
        }
    }
});

Chaining handlers

Is there any way to chain handlers using this library? I tried to but after I return calling next() the next function doesn't get called.

router.get(
    '/foo/:id',
    function(req, res, next) {
        console.log('Authenticate!');
        return next();
    },
    function(req, res, next) {
        res.send(200);
        return next();
    }
);

Problem with nested functions via router.add

I'm experiencing an issue when I updated restify to the last version.

Some routes that I have added via de .add function seems to not being added to the server and it responds with:
{ "code": "ResourceNotFound", "message": "/api-partners/panel does not exist" }

I've made an example repo here: https://github.com/Cuxs/restify-router-error-sample

To further explain the problem, i have this folders:
image

Each of those x.routes have this code:

const { Router } = require('restify-router');

const routes = new Router();

const handleRouteA= (req,res)=>res.send({status:'ok', message:'Route A'})

routes.get('/', handleRouteA);

module.exports = { routes };

And this is the code of the routes.js file:

const { Router } = require('restify-router');
const { routes: aRoutes } = require('./a/a.routes');
const { routes: bRoutes } = require('./b/b.routes');
const { routes: cRoutes } = require('./c/c.routes');

const routes = new Router();

const handleAnotherRoute = (req,res)=>res.send(401)

routes.add('/panel/a', aRoutes); // businessUser module routes.
routes.add('/panel/b', bRoutes); // businessUser module routes.
routes.add('/panel/c', cRoutes); // businessUser module routes.

routes.get('/another-route', handleAnotherRoute)

module.exports = { routes };

Using the add function, I added that routes to my index.js file:

//...
router.add('/api-partners', moreRoutes.routes);
//...

If I try to fetch to /api-partners/another-route the response I get is:
image

This is correct, but if I try to fetch /api-partners/panel/a the response I get is:

image

I don't know what might be causing this, this problem appears when I updated restify to it's last version (@8.5.1), I update restify-router too, but that not seems to fix the problem.

Hope I explained myself. Thanks in advance!

Data in body is not parsed

I am sending the body as a JSON and I put the relevant plugins in restify

When I add the "testify-routes" the req.params is not parsing the body of the request so I am not able to use them

Regards

Use joi with restify-router

I am trying to add a validation using joi like this

sme.get('/country/:country/state/:state/city/:city',validator, function(req, res, next) {
})

Can you please comment if it is possible to use joi adaptor like restify-joi-middleware
with restify-router

thanks
tuhin

Upload file with multer

When I use with only restify
I use:

server.post('/upload', uploads.single('foto'), async (req, res, next)=>{
    
      try {
          res.send( saveObj  )
      } catch (error) {
          res.send(error)
      }
      next()
  })

But if I use with restify-router, I get this wrong

Argument of type 'RequestHandler' is not assignable to parameter of type 'RequestHandlerType'.
Type 'RequestHandler' is not assignable to type 'RequestHandler'.
Types of parameters 'req' and 'req' are incompatible.
Type 'Request' is missing the following properties from type 'Request<ParamsDictionary, any, any>': get, acceptsCharsets, acceptsEncodings, acceptsLanguages, and 20 more.ts(2345)

issue

Is possible to do upload with restify-router?

Recursive routers

I'm very used to the Express-way of handling routes. Is is possible to make these routers use another router?

The Express-way I'm talking about is as following:

// routes/routes.js
router.use("/v1", require("./v1/routes"));

// routes/v1/routes.js
router.use("/auth", require("./auth"));

// routes/v1/auth.js
router.post("/register", function (req, res, next) {});

The following code, in Express, would allow me to call POST /v1/auth/register and it'd simply work. Is the same possible using this module?

Support object as first argument when defining methods for a Route

Currently, a string is used as first argument to any Route method (get, put, post, etc.)
This prevents us from supplying version and other parameters to a route (or at least I have not found a way in current version of restify-router).

Please, add this feature or tell me if you encourage PRs. I'll try to do my best in this case :)

Problem in combination with node-restify-validation npm module

Hi.

I was using restify-router alone and then I have added node-restify-validation module and now I am getting errors. I am using nested routers with routes folder, v1 subfolder and auth.js route file.

The excerpt from auth.js file

router.post({ url: '/register', validation: {
    content: {
        name: { isRequired: true },
        email: { isRequired: true, isEmail: true },
        password: { isRequired: true }
    }
}}, function (req, res, next) {

Error:

TypeError: path must be string or RegExp
    at toRegex (/node_modules/restify-router/lib/path.js:19:9)
    at concat (/node_modules/restify-router/lib/path.js:59:16)
    at /node_modules/restify-router/lib/router.js:131:24
    at Array.forEach (<anonymous>)
    at /node_modules/restify-router/lib/router.js:127:27
    at Array.forEach (<anonymous>)
    at Router.applyRoutes (/node_modules/restify-router/lib/router.js:122:11)
    at /node_modules/restify-router/lib/router.js:144:12
    at Array.forEach (<anonymous>)
    at Router.applyRoutes (/node_modules/restify-router/lib/router.js:139:16)
    at /node_modules/restify-router/lib/router.js:144:12
    at Array.forEach (<anonymous>)
    at Router.applyRoutes (/node_modules/restify-router/lib/router.js:139:16)
    at NativeConnection.db.once (server.js:39:16)
    at Object.onceWrapper (events.js:273:13)
    at NativeConnection.emit (events.js:182:13)

I already did some console.log outputs and I am getting in the toRegex function in path.js

/v1/auth
undefined

I think the problem is it is not picking up the url property.

Best regards.

Nested routers don't process commonHandlers in order

If we use nested routers each with their own set of commonHandlers, the middleware of the deepest router executes first.

it('Should process middlewares in order', function (done) {
      var v1 = new Router();
      var auth = new Router();
      var register = new Router();

      var first = function (req, res, next) {
        req.test = [1];
        next();
      };

      var second = function (req, res, next) {
        req.test.push(2);
        next();
      };

      var third = function (req, res, next) {
        req.test.push(3);
        next();
      };

      register.post('/register', function (req, res, next) {
        res.send({status: 'success', name: req.body.name, commonHandlerInjectedValues: req.test});
        return next();
      });

      auth.use(second, third)
      auth.add('/auth', register);
      v1.use(first)
      v1.add('/v1', auth);

      v1.applyRoutes(server);

      request(server)
        .post('/v1/auth/register')
        .set('Content-Type', 'application/json')
        .send({name: 'test'})
        .expect(200)
        .end(function (err, res) {
          if (err) {
            return done(err);
          }

          res.body.should.deep.equal({status: 'success', name: 'test', commonHandlerInjectedValues: [1,2,3]});
          done();
        });

    });

The test case would fail.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.