Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@e22m4u/js-trie-router
Advanced tools
A pure ES-module of the Node.js HTTP router that uses the Trie for routing.
preHandler
and postHandler
hooks.npm install @e22m4u/js-trie-router
A basic "Hello world." example.
import http from 'http';
import {TrieRouter} from '../src/index.js';
import {HTTP_METHOD} from '../src/route.js';
const server = new http.Server(); // A Node.js HTTP server.
const router = new TrieRouter(); // A TrieRouter instance.
router.defineRoute({
method: HTTP_METHOD.GET, // Request method.
path: '/', // Path template like "/user/:id".
handler(ctx) { // Request handler.
return 'Hello world!';
},
});
server.on('request', router.requestHandler);
server.listen(3000, 'localhost');
// Open in browser http://localhost:3000
The first parameter of the Router
handler is the RequestContext
instance.
container: ServiceContainer
req: IncomingMessage
res: ServerResponse
query: ParsedQuery
headers: ParsedHeaders
cookie: ParsedCookie
The RequestContext
can be destructured.
router.defineRoute({
// ...
handler({req, res, query, headers, cookie}) {
console.log(req); // IncomingMessage
console.log(res); // ServerResponse
console.log(query); // {id: '10', ...}
console.log(headers); // {'cookie': 'foo=bar', ...}
console.log(cookie); // {foo: 'bar', ...}
// ...
},
});
Return values of the Route
handler will be sent as described below.
type | content-type |
---|---|
string | text/plain |
number | application/json |
boolean | application/json |
object | application/json |
Buffer | application/octet-stream |
Stream | application/octet-stream |
Here is an example of a JSON response.
router.defineRoute({
// ...
handler(ctx) {
// sends "application/json"
return {foo: 'bar'};
},
});
If the ServerResponse
has been sent manually, then the return
value will be ignored.
router.defineRoute({
// ...
handler(ctx) {
res.statusCode = 404;
res.setHeader('content-type', 'text/plain; charset=utf-8');
res.end('404 Not Found', 'utf-8');
},
});
Set environment variable DEBUG=jsTrieRouter*
before start.
DEBUG=jsPathTrie* npm run test
npm run test
MIT
FAQs
HTTP router for Node.js based on a prefix tree
The npm package @e22m4u/js-trie-router receives a total of 141 weekly downloads. As such, @e22m4u/js-trie-router popularity was classified as not popular.
We found that @e22m4u/js-trie-router demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.