Code Monkey home page Code Monkey logo

Comments (18)

lirantal avatar lirantal commented on June 2, 2024

Indeed, however you could provide the routesMap with an array and work with arrays of function callbacks like this:

var cb0 = function (req, res, next) {
  console.log('CB0')
  next()
}

var cb1 = function (req, res, next) {
  console.log('CB1')
  next()
}

var cb2 = function (req, res) {
  res.send('Hello from C!')
}

app.get('/example/c', [cb0, cb1, cb2])

The source for that is from Express's official docs.

We'll need to apply a change to the versionRoutes.route() static function to identify if the defined set value is an array and if so then to just return it.

What do you think about proposing a Pull Request for that change?

from express-version-route.

arvindgemini avatar arvindgemini commented on June 2, 2024

@LiranBri Yes please make the changes to accept multiple callback in the form of array.

from express-version-route.

arvindgemini avatar arvindgemini commented on June 2, 2024

@LiranBri

I have made the changes to accept array of callback and returning versionRouter as it is in case of array. We are not getting the response of API. Please find code changes below.

express-version-route/index.js
for (let [versionKey, versionRouter] of versionsMap) {
versionArray.push(versionKey)
if (this.checkVersionMatch(req.version, versionKey)) {
if(Array.isArray(versionRouter))
return versionRouter;
else
return versionRouter(req, res, next)
}
}

Router config
var cb0 = function(req, res, next) {
console.log('CB0');
next();
};

var cb1 = function(req, res, next) {
console.log('CB1');
next();
};

var cb2 = function(req, res) {
res.send('Hello from C!');
};

routesMap.set('2.0', [cb0, cb1, cb2]);

When I am making request it is getting timeout. any suggestions ?

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

It looks like your change should work, if you're indeed calling next() on each of the array elements until the last one.

@arvindgemini can you actually create a pull request? it would be a lot easier to understand what you're trying to do that isn't working.

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

@arvindgemini instead of the pull request you sent, can you please try the following code and let me know if this way of array middleware works for you?

var app = express()
var router = express.Router()

var cb1 = function (req, res, next) {
  console.log('CB1')
  next()
}

var cb2 = function (req, res, next) {
  console.log('CB2')
  next()
}

var cb3 = function (req, res) {
  res.send('Hello from CB3')
}

const middlewaresArray = router.use([cb1, cb2, cb3])
app.get('/example/c', middlewaresArray)

from express-version-route.

arvindgemini avatar arvindgemini commented on June 2, 2024

@lirantal router.use will use all the methods as middle-ware, because of this we have to create router object for each version and each route. Do you think this is a good approach ?

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

Well, you can replace router.use() with router.get() or post, or any other specific methods you'd want.

from express-version-route.

arvindgemini avatar arvindgemini commented on June 2, 2024

@lirantal
Please find my code sample below
const routesMap = new Map<string, Handler>();
const route = Router();
export default (app: Router) => {
app.use('/version-customer', route);

var cb1 = function(req, res, next) {
console.log('CB1');
next();
};

var cb2 = function(req, res, next) {
console.log('CB2');
next();
};

var cb3 = function(req, res) {
res.send('Hello from CB3');
};

const middlewaresArray1 = route.get('', [cb1, cb2, cb3]);
const middlewaresArray2 = route.get('', [cb1, cb2]);
routesMap.set('1.0', middlewaresArray1);
routesMap.set('2.0', middlewaresArray2);

app.get('/example/c', versionRouter.route(routesMap));
};

If I use route.get() then getting 404 route not found issue whereas If I use route.use route is working, But getting response Hello from CB3 for version 2.0 also.

Please share a working example of a route with two version and each version using 3 functions to handle the response.

from express-version-route.

arvindgemini avatar arvindgemini commented on June 2, 2024

@lirantal Any update?

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

@arvindgemini this is a complete example that shows how it works.

const express = require("express")
const version = require("express-version-route")
const versionRequest = require("express-version-request")

const app = express()
app.use(versionRequest.setVersionByQueryParam('v'))

const routesMap = new Map()

const cb1 = (req, res, next) => {
  console.log('CB1')
  res.status(200).send(200)
};

const cb2 = (req, res, next) => {
  console.log('CB2')
  res.status(200).send()
}

var ex1 = function (req, res, next) {
  console.log('EX1')
  next()
}

var ex2 = function (req, res, next) {
  console.log('EX2')
  next()
}

var ex3 = function (req, res, next) {
  res.status(200).send('Hello from EX3')
}

var multiMiddlewareRouter = express.Router()
multiMiddlewareRouter.use([ex1,ex2, ex3])

routesMap.set("1.0", multiMiddlewareRouter);
routesMap.set("2.0", cb2);

app.get('/dogs', version.route(routesMap));

app.use((req, res) => {
  res.status(404).send()
})

app.use((err, req, res, next) => {
  res.status(500).json({
    errors: [
      {
        status: 500,
        title: "Server error.",
        detail: `This error occurred on the server: ${err.stack}`,
      },
    ],
  });
});

module.exports = app;

To test, you can run:

curl http://localhost:4000/dogs\?v\=1

which will trigger all 3 middleware and return the result of the last one

and

curl http://localhost:4000/dogs\?v\=2

Which will just trigger the single callback middleware for version 2

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

I don't know why you had issues before using the router.use(). That's how it is intended to be used.

If you want to be specific, you can also define it like this:

var multiMiddlewareRouter = express.Router()
multiMiddlewareRouter.get('/dogs', [ex1,ex2, ex3])

But then you need to provide exact paths (like /dogs there) to each of the multiMiddlewareRouter that you use.

from express-version-route.

ChrisMaze avatar ChrisMaze commented on June 2, 2024

Any updates for this issue???

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

@ChrisMaze what sort of updates are you looking for? this should work well as-is

from express-version-route.

ChrisMaze avatar ChrisMaze commented on June 2, 2024

Hi @lirantal
I'm looking for using an array when set the routeMap, just like that

const routesMap = new Map<string, Handler | Handler[]>([
  [API_VERSION.DEFAULT, [middleware-1, middleware-2, middleware-3]],
  [API_VERSION.V1,  [middleware-1, middleware-2, middleware-3]],
  [API_VERSION.V2,  [middleware-2, middleware-4, middleware-5]],
]);

But that does not work, since versionRouter.route does not support accepting a handler array. it directly to call the callback handler func. so I have to create a router for that, such as:

const routerV1 = Router().get('*', [middleware-1, middleware-2, middleware-3])
const routerV2 = Router().get('*', [middleware-2, middleware-4, middleware-5])
const routesMap = new Map<string, Handler>([
  [API_VERSION.DEFAULT, routerV1],
  [API_VERSION.V1, routerV1],
  [API_VERSION.V2,  routerV2],
])

Actually, that makes my code not clean enough, we should NOT use the router in this way.
And I noticed there has a pr
#60 1 year ago but not merged.

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

@ChrisMaze if you're able to add test cases to that PR or create your own new one with tests then I'm happy to help out with this.

from express-version-route.

ChrisMaze avatar ChrisMaze commented on June 2, 2024

I went through the code and found that pr is not right.
Since your codebase is trying to run the handler directly, so I have to refactor lots of code to make it support the array.
I will not do it here, since I don't like the solution for the core feature, I will find another way internally

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

Sounds good then. Go ahead and make the changes and submit a PR. I'll be happy to merge it once you do.

from express-version-route.

lirantal avatar lirantal commented on June 2, 2024

Per comment above, expecting new PR to be able to merge it.

from express-version-route.

Related Issues (12)

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.