HTTP-маршрутизатор статичных ресурсов для Node.js
|
|
7 часов назад | |
|---|---|---|
| .husky | 7 часов назад | |
| dist | 7 часов назад | |
| example | 7 часов назад | |
| src | 7 часов назад | |
| .c8rc | 7 часов назад | |
| .commitlintrc | 7 часов назад | |
| .editorconfig | 7 часов назад | |
| .gitignore | 7 часов назад | |
| .mocharc.json | 7 часов назад | |
| .prettierrc | 7 часов назад | |
| LICENSE | 7 часов назад | |
| README.md | 7 часов назад | |
| build-cjs.js | 7 часов назад | |
| eslint.config.js | 7 часов назад | |
| package.json | 7 часов назад | |
| tsconfig.json | 7 часов назад |
HTTP-маршрутизатор статичных ресурсов для Node.js.
npm install @e22m4u/js-http-static-router
Модуль поддерживает ESM и CommonJS стандарты.
ESM
import {HttpStaticRouter} from '@e22m4u/js-http-static-router';
CommonJS
const {HttpStaticRouter} = require('@e22m4u/js-http-static-router');
import http from 'http';
import {HttpStaticRouter} from '@e22m4u/js-http-static-router';
// создание экземпляра маршрутизатора
const staticRouter = new HttpStaticRouter();
// определение директории "../static"
// доступной по адресу "/static"
staticRouter.addRoute(
'/static', // путь маршрута
`${import.meta.dirname}/../static`, // файловый путь
);
// объявление файла "./static/file.txt"
// доступным по адресу "/static"
staticRouter.addRoute(
'/file.txt',
`${import.meta.dirname}/static/file.txt`,
);
// создание HTTP сервера и подключение обработчика
const server = new http.Server();
server.on('request', (req, res) => {
// если статический маршрут найден,
// выполняется поиск и отдача файла
const staticRoute = staticRouter.matchRoute(req);
if (staticRoute) {
return staticRouter.sendFileByRoute(staticRoute, req, res);
}
// в противном случае запрос обрабатывается
// основной логикой приложения
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from App!');
});
server.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
console.log('Try to open:');
console.log('http://localhost:3000/static/');
console.log('http://localhost:3000/file.txt');
});
npm run test
MIT