ukayani / restify-router Goto Github PK
View Code? Open in Web Editor NEWA router interface for restify that lets you aggregate route definitions and apply to a restify server
License: MIT License
A router interface for restify that lets you aggregate route definitions and apply to a restify server
License: MIT License
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
});
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
}
}
}
});
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();
}
);
Something like:
require('./app/routers/product.router').applyRoutes(server, '/api/v1', [middleWareFunctions]);
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:
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:
This is correct, but if I try to fetch /api-partners/panel/a
the response I get is:
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!
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
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
I was trying to debug why my regexed route was not working. but it only accepted it from the base address, and not from the /v1/.../route. Maybe a good enhancement would be appending the regex?
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)
Is possible to do upload with restify-router?
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?
For better (less code) version and routing management:
Gist
If you do this, u can add a router.clone() or router.copy() thats return self?
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 :)
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.
Wondering if the repo owner has plans to continue work on this?
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.