hyper-routing
v0.0.1
Published
An amazing and simple route manager
Downloads
1
Readme
Hyper-Routing
An amazing and simple route manager
Instalation
npm i hyper-routing
Table of contents
Usage
// Using Node.js `require()`
const hyperRouting = require('hyper-routing');
// Using ES6 imports
import hyperRouting from 'hyper-routing';
const app = new hyperRouting()
Application instance
<method>(path, ...callbacks)
app.get('/', (req, res, next) => {
// Do something
});
app.post('/', (req, res, next) => {
// Do something
});
use(middleware)
Uses a middleware function in all routes.
/* Creating own middleware */
const myOwnMiddleware = (req, res, next) => {
console.log("Hello world!")
console.log(`I was called on ${req.url}`)
next(); // Important to pass control to next middleware function
}
app.use(myOwnMiddleware)
listen(port, callback)
Starts the server in the desired port.
app.listen(3000, () => {
// Do something
});
Middlewares
bodyparser({...parser})
Parses the req data into object readable data.
app.use(app.bodyparser()) // Will parse both application/json and www-x-form-urlencoded data
app.use(app.bodyparser({ json: true })) // Will only parse application-json data
app.use(app.bodyparser({ urlencoded: true })) // Will only parse www-x-form-urlencoded data
Request
body
Attribute of the Request class containing the <input/>
values of the <form/>
with key based in their names
attribute.
<form action="/login">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit">Login</button>
</form>
{
username: "someusername",
password: "somepassword"
}
app.post('/login', (req, res) => {
const { username, password } = req.body;
});
params
Attribute of the Request class with the specified parameters in the path.
// http://localhost:3000/users/12345
{
id: "123458"
}
// http://localhost:3000/users/12345
app.get('/users/:id', (req, res) => {
const { id } = req.params;
console.log(id); // Logs 123458
});
query
Attribute of the Request class with the query parameters in the url.
// http://localhost:3000/products?category=clothes&design=fancy&sort=recent
{
category: "clothes",
design: "fancy",
sort: "recent"
}
// http://localhost:3000/products?category=clothes&design=fancy&sort=recent
app.get('/products', (req, res) => {
const { category, design, sort } = req.query;
console.log(category); // Logs clothes
console.log(design); // Logs fancy
console.log(sort); // Logs recent
});
Response
status(code)
Sets the response status code.
app.get('/', (req, res) => {
res.status(404) // Status Code: 404
res.status("Not Found") // Status Code: 404
});
json(data)
Sends an application/json
content-type response.
app.get('/', (req, res) => {
res.json({
username: "Álvaro",
age: "16"
}) // Will display a JSON object
});
send(data)
Sends a text/plain
content-type response.
app.get('/', (req, res) => {
res.send("An amazing webpage :)")
});
app.get('/', (req, res) => {
res.send({ // As this is an object it will trigger res.json()
data: "An amazing webpage :)"
})
});
sendFile(path)
Sends a text/html
content-type response.
app.get('/', (req, res) => {
res.sendFile("index.html")
});
redirect(url)
Redirects to the specified url.
app.get('/profile', (req, res) => {
if(!req.user) { // Using some type of auth
res.redirect('/login'); // Redirects the user to http://localhost:3000/login
};
});
Examples
Check out the examples to see how you can use hyper-routing
in a project.
Help
Have any doubts or suggestions? Send me a private message on Discord: @newalvaro9
License
Copyright 2023, Álvaro Poblador. Inspired in express.js