Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@jaris/router
Advanced tools
jaris is still in very early alpha, production usage is not recommended
Router for @jaris/core
$ npm install -S @jaris/core @jaris/router
import server, { text } from '@jaris/core';
import router, { get } from '@jaris/router';
server([router([get('/', conn => text('Hello, world!', conn))])]);
// user.controller.ts
import { json } from '@jaris/core';
const userController = {
// method can be async!
index: async conn => {
// fetch users, etc
return json({ users: [] }, conn);
},
};
export default userController;
// api.routes.ts
import { get } from '@jaris/router';
import userController from './user.controller';
const apiRoutes = [get('/users', userController.index)];
export default apiRoutes;
// index.ts
import server from '@jaris/core';
import router from '@jaris/router';
import apiRoutes from './api.routes.ts';
server([
(conn) => {
console.log('Since the router is also just a middleware itself, we can have as many middleware before or after that we want!');
return conn;
}
router(apiRoutes)
])
Route parameters are defined using a colon in the route definition and are set as object values on conn.params
.
import server, { text } from '@jaris/core';
import router, { get } from '@jaris/router';
server([
router([
get('/users/:userId', conn =>
text(`Hello, user ${conn.params.userId}!`, conn),
),
]),
]);
Prefixes & Middleware
import server, { json, status, halt } from '@jaris/core';
import router, { get, post, group } from '@jaris/router';
// Middleware are the same as @jaris/core
// so they need to follow the same rule
// of returning a new connection
const companyMiddleware = conn => {
const token = conn.headers['Authorization'];
// ... parse token
// fetch user it belongs to
// check if user has access to company
// if you want to continue, return the connection
if (userHasAccess) {
return conn;
}
// otherwise we set errors and tell jaris
// to stop by using the "halt" helper
return pipe(
status(403),
json({ error: 'You do not have permission' }),
halt,
)(conn);
};
server([
router([
// groups need to be spread since
// they return an array of routes
...group({ prefix: 'v1' }, () => [
// will evaluate to /v1/users
get('/users', userController.index),
// leading / trailing slashes are optional
post('users', userController.store),
// groups can be nested
...group(
{
prefix: '/companies/:companyId',
middleware: [companyMiddleware],
},
() => [
// /v1/companies/:companyUid
get('/', companyController.show),
],
),
]),
]),
]);
FAQs
> TODO: description
We found that @jaris/router 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.