Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jojobyte/theway
A tiny (zero dependency) isomorphic router for browsers & servers.
https://github.com/jojobyte/theway
backend browser frontend isomorphic isomorphic-javascript router server vanilla vanilla-js zero-dependencies zero-dependency
Last synced: 3 months ago
JSON representation
A tiny (zero dependency) isomorphic router for browsers & servers.
- Host: GitHub
- URL: https://github.com/jojobyte/theway
- Owner: jojobyte
- Created: 2024-09-06T02:28:46.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2024-10-16T13:07:27.000Z (4 months ago)
- Last Synced: 2024-10-19T06:51:19.014Z (4 months ago)
- Topics: backend, browser, frontend, isomorphic, isomorphic-javascript, router, server, vanilla, vanilla-js, zero-dependencies, zero-dependency
- Language: JavaScript
- Homepage:
- Size: 36.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# The Way
A tiny (zero dependency) isomorphic router for browsers & servers.
- History & Hash based routing for browser
- Server-side (express style) routing (req,res,next) & middleware
- Simple Static file server
- Support for layouts### Install
```sh
npm install theway
```### Usage
```js
// app.js
import Way from 'theway'
import {
loadRoute,
} from 'theway/utils.js'const router = new Way('/');
export const createApp = (entrypoint, routeBase) => router
.use("/", loadRoute('/path/to/route/home.js', 'home'))
.use(
"/about",
loadRoute('/path/to/route/about.js', 'about'),
)
.use(
"/thing/*?",
loadRoute('/path/to/route/thing.js', 'thing'),
)
.use(
"/compare/:crypto?/:fiat?",
loadRoute('/path/to/route/compare.js', 'compare'),
)export default createApp
``````js
// main.js - browser
import createApp from './app.js'let app = createApp(
document.querySelector('#app')
)app.listen();
``````js
// server.js
import http from 'node:http'
import { readFileSync } from 'node:fs'
import { readFile, writeFile } from 'node:fs/promises'
import { join, extname } from 'node:path'import createApp from './app.js'
import {
DOMFaker,
serveStaticFiles,
} from 'theway/server.js'const BASE = //ig
const routeBase = import.meta?.dirname + '/routes/'
const theWaySrcDir = join(import.meta.dirname, '../../')
const entryPage = readFileSync('./index.html', 'utf8').replace(BASE, '')
const fakeDOM = DOMFaker(``, entryPage)const httpServer = http.createServer();
const app = createApp(
fakeDOM,
routeBase,
)app
.get("/api/users", async ({params}, res, next) => {
try {
let req = await fetch(
'https://dummyjson.com/users?limit=3&select=firstName,age',
{
headers: {
'Content-Type': 'application/json',
},
}
)if (req.ok) {
let users = await req.text()if (users) {
console.log('router get /api/users', params, users)return res.json(users)
}
}
} catch (err) {
next(err)
}next('failed to retrieve users data')
})
.use(serveStaticFiles(join(import.meta.dirname, '../')))httpServer.on('request', app.listen);
httpServer.listen(8080, () => {
console.log('Listening on http://127.0.0.1:8080');
});
```---
### Special Thanks
Originally forked from [NavAid](https://github.com/lukeed/navaid), but with significant changes.