Gathering detailed insights and metrics for als-static-routes
Gathering detailed insights and metrics for als-static-routes
Gathering detailed insights and metrics for als-static-routes
Gathering detailed insights and metrics for als-static-routes
npm install als-static-routes
Typescript
Module System
Node Version
NPM Version
47.7
Supply Chain
54.8
Quality
75.8
Maintenance
100
Vulnerability
98.4
License
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
66.7%
5
Compared to previous week
Last month
23.5%
21
Compared to previous month
Last year
0%
675
Compared to previous year
4
als-static-routes
is a utility for handling static files in web applications, enabling efficient static content management. It integrates seamlessly with frameworks like Express, but can also operate as a standalone static file server.
als-uni-store
).Install the package using npm:
1npm install als-static-routes
1const StaticRouter = require('als-static-routes'); 2const staticRouter = new StaticRouter(__dirname); 3staticRouter.add('/', 'public'); 4 5const http = require('http'); 6const server = http.createServer(staticRouter.handler); 7server.listen(3000);
Or
1const StaticRouter = require('als-static-routes'); 2const staticRouter = new StaticRouter(__dirname); 3staticRouter.add('/', 'public') 4 5const http = require('http'); 6const server = http.createServer((req,res) => { 7 staticRouter.handler(req,res,(req,res) => { 8 // if no static files response with something else 9 res.end('Some another route') 10 }) 11}) 12server.listen(3000);
1const StaticRouter = require('als-static-routes'); 2const staticRouter = new StaticRouter(__dirname); 3staticRouter.add('/', 'public'); 4 5const express = require('express'); 6const app = express(); 7app.use(staticRouter.handler); 8app.listen(3000);
Create a new router:
1const options = { 2 index: true, // Serve 'index.html' files for directory routes 3 download: false, // Send files with 'Content-Disposition: attachment' 4 etag: true, // Enable ETag header for caching optimization 5 charset: true // Append charset to 'Content-Type' header based on file content and mime type 6}; 7 8const router = new StaticRouter(rootDir, options);
Add routes for serving files or directories:
url
: URL path to serve the file or directory.path
: Path relative to rootDir
.options
: Overrides constructor options for specific routes.Throwing errors when:
Examples:
1router.add('/','public',{index:false}) 2router.add('/route-to-file','/some/file.txt') 3router.add('/file-for-download','/some/file.txt',{download:true}) // download link
Each add, updates previous adds.
You can remove specific route or group of routes, by passing as argument url for routes which starts with this url.
Remove a file or directory from being served:
1await router.remove('/path/to/resource');
Handler function to integrate with Node.js HTTP server or Express:
1const server = http.createServer((req, res) => { 2 router.handler(req, res, () => { 3 res.writeHead(404); 4 res.end('Not Found'); 5 }); 6});
If next
is not provided, a default 404 handler is used.
The handler by default:
1next = () => { 2 res.writeHead(404); 3 res.end('Not Found'); 4}
1staticRouter.store // instance of DiskStore(als-uni-store) for managing static files 2staticRouter.routes // routes
No vulnerabilities found.
No security vulnerabilities found.