Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
grapnel-server
Advanced tools
Node.js Router Framework with Named Parameter & Middleware support
Install with npm
npm install grapnel-server
var http = require('http'),
app = require('grapnel-server');
app.get('/products/:category/:id?', function(req, res, next){
var id = req.params.id,
category = req.params.category
console.log(category, id);
res.end('Hello World!', 200);
});
http.createServer(app.start()).listen(3000);
Grapnel.js supports regex style routes similar to Sinatra, Restify, and Express. The properties are mapped to the parameters in the request.
app.get('products/:id?', function(req, res){
// GET /products/134
req.params.id
// => 134
});
app.get('products/*', function(req, res){
// The wildcard/asterisk will match anything after that point in the URL
// Parameters are provided req.params using req.params[n], where n is the nth capture
});
Grapnel.js also supports middleware:
var auth = function(req, res, next){
user.auth(function(err){
req.user = this;
next();
});
}
app.get('/*', auth, function(req, res){
console.log(req.user);
res.end('Hello ' + req.user.name, 200);
});
You can add context to a route and even use it with middleware:
var usersRoute = router.context('/user/:id', getUser, getFollowers); // Middleware can be used here
usersRoute.get('/', function(req, res, next){
console.log('Profile', req.params.id);
});
usersRoute.get('/followers', otherMiddleware, function(req, res, next){ // Middleware can be used here too
console.log('Followers', req.params.id);
});
usersRoute.post('/', function(req, res, next){ // Middleware can be used here too
console.log('POSTED to /user/:id', req.params.id);
});
// GET /user/13589
// => Profile 13589
// GET /user/13589/followers
// => Followers 13589
// POST /user/13589
// => POSTED to 13589
var Grapnel = require('grapnel-server').Server;
var routes = {
'products' : function(req, res, next){
// GET /products
},
'products/:category/:id?' : function(req, res, next){
// GET /products/widgets/35
req.params.category
// => widgets
}
}
Grapnel.listen(routes);
var app = require('grapnel-server');
app.on('navigate', function(){
// GET /foo/bar
console.log('URL changed to %s', this.path());
// => URL changed to /foo/bar
});
Grapnel.js allows RegEx when defining a route:
var expression = /^\/food\/tacos\/(.*)$/i;
app.get(expression, function(req, res){
// GET /food/tacos/good
console.log('I think tacos are %s.', req.params[0]);
// => "He thinks tacos are good."
});
Grapnel uses middleware similar to how Express uses middleware. Middleware has access to the req
object, event
object, and the next middleware in the call stack (commonly denoted as next
). Middleware must call next()
to pass control to the next middleware, otherwise the router will stop.
For more information about how middleware works, see Using Middleware.
var user = function(req, res, next){
user.get(function(err){
req.user = this;
next();
});
}
app.get('/user/*', user, function(req, res){
console.log(req.user);
res.send(req.user.name);
});
app.on('match', function(req){
req.event.preventDefault(); // Stops event handler
});
app.get('/products/:id', function(req, res){
req.event.stopPropagation(); // Stops propagation of the event
});
app.get('/products/widgets', function(req, res){
// This will not be executed
});
// GET /products/35
You can specify a route that only uses a wildcard *
as your final route, then use req.event.parent()
which returns false
if the call stack didn't run any route handlers.
// This should be your last route
app.all('*', function(req, res){
if(!this.state.parent()){
res.writeHead(404);
res.end('404');
}
});
get
, post
, put
, delete
(HTTP Method) Adds a listeners and middleware for routes matching its respective HTTP method/**
* @param {String|RegExp} path
* @param {Function} [[middleware], callback]
*/
app.get('/store/:category/:id?', function(req, res){
var category = req.params.category,
id = req.params.id;
console.log('Product #%s in %s', id, category);
});
app.post('/store/:category', function(req, res){
var category = req.params.category;
console.log('POST Product %s', category);
});
app.put('/store/:category', function(req, res){
var category = req.params.category,
id = req.params.id;
console.log('PUT Product #%s in %s', id, category);
});
app.delete('/store/:category/:id', function(req, res){
var category = req.params.category,
id = req.params.id;
console.log('DELETE Product #%s in %s', id, category);
});
app.all('/store/', function(req, res){
// This will be called with any HTTP method
});
on
Adds a new event listener/**
* @param {String} event name (multiple events can be called when separated by a space " ")
* @param {Function} callback
*/
router.on('myevent', function(event){
console.log('Grapnel.js works!');
});
once
A version of on
except its handler will only be called once/**
* @param {String} event name (multiple events can be called when separated by a space " ")
* @param {Function} callback
*/
router.once('init', function(){
console.log('This will only be executed once');
});
trigger
Triggers an event/**
* @param {String} event name
* @param {Mixed} [attributes] Parameters that will be applied to event handler
*/
app.trigger('event', eventArg1, eventArg2, etc);
context
Returns a function that can be called with a specific route in context.Both the router.context
method and the function it returns can accept middleware. Additionally, you can specify which HTTP method (GET, POST, PUT, DELETE) should be routed by the callback. Not specifying an HTTP method will assume GET.
Note: when calling route.context
, you should omit the trailing slash.
/**
* @param {String} Route context (without trailing slash)
* @param {[Function]} Middleware (optional)
* @return {Function} Adds route to context
*/
var usersRoute = router.context('/user/:id');
usersRoute.post('/', function(req, res, next){
console.log('POSTED to', req.params.id);
});
// Not specifying an HTTP method assumes GET
usersRoute('/followers', function(req, res, next){
console.log('Followers', req.params.id);
});
// GET /user/13589/followers
// => Followers 13589
// POST /user/13589
// => Followers 13589
bind
An alias of on
add
An alias of get
path
router.path('string')
Sets a new path or hashrouter.path()
Gets path or hashrouter.path(false)
Clears the path or hashfragment
(Deprecated)navigate
Fires when http module initializes a new requestmatch
Fires when a new match is found, but before the handler is calledFAQs
Node.js Router Framework with Named Parameter & Middleware support
We found that grapnel-server demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.