0.3.1 • Published 4 years ago

htroute v0.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

HTRoute

Code size Code Climate Maintability Open issues Discord npm version MIT License

HTRoute is a minimalistic HTTP routing library for Node.js.

npm install htroute

const htroute = require('htroute');
const app     = new HTRoute();

app.add('/', ['GET'], function() {
    return 'Hello, World!';
});

app.serve(8080, (port) => console.log(`HTRoute app listening on port ${port}!`));

Documentation

This is the API documentation. If you want to know how to get started, read "GETTING_STARTED.md". There are also a lot of examples in "/examples".

The HTRoute class has the following methods:

add(path, method, callback)

This method adds routes which the user can navigate to to your app. path is the address relative from your host, method is an array of all accepted HTTP methods (GET, POST, ...) and callback is the function which will be executed to render the site that will be sent to the client. Example:

// The library passes two arguments to your callbacks:
// 1.: The client request
// 2.: The server response
// Your function doesn't need to use them. You can just leave them out if you don't need them.
// The callback's return value will be passed back to the client as the response body:
app.add('/', ['GET'], function(req, res) {
    return 'Hello, World!';
});

addRegex(path, method, callback)

This method is used to create routes "dynamically". Everytime a request path matches your RegExp, the callback will be executed:

app.addRegex(/test.*/, ['GET'], function(req) { // This RegExp will match paths like "/test/hello", "/test/12345/world/", ...
    return `Welcome! You're at ${req.url}.`;
});

handleError(code, callback)

This method is used to override the default error pages. code represents the HTTP status code which will be handled, callback is the function which will be executed when the error happens:

// You don't need to set the status code yourself.
// There are two arguments passed to your callback:
// 1.: The client request
// 2.: The server response
// Your function doesn't need to use them. You can just leave them out if you don't need them.
// The callback's return value will be passed back to the client as the response body:
app.handleError(404, function(req, res) {
    return 'There is no route for ' + req.url + '!';
});

serve(port, callback)

This method starts the server. port is the port your app will run on. callback is an optional function that will be executed when the server has been started successfully. Example:

// Port: 8080
// Callback: Logs "HTRoute app listening on port 8080!"
app.serve(8080, (port) => console.log(`HTRoute app listening on port ${port}!`));

Configuration options

You can configure the directory where all your static files are located and the content of the header "X-Powered-By". You don't need to set them, they are optional:

const app = new HTRoute({
    'staticDir': 'public'   // The directory where all your static files are located
    'poweredBy': 'Ferrets'  // The content of the header "X-Powered-By"
});

License

HTRoute is licensed under the MIT License (see LICENSE for details).

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago