@hellomelo/expressplus
v1.1.3
Published
Simple framework for creating websites with ease.
Downloads
6
Readme
ExpressPlus
ExpressPlus is a simple framework designed to create webpages with ease.
Routing
Creating a new ExpressPlus instance, with the port being 3000:
const ExpressPlus = require('@hellomelo/expressplus');
const exp = new ExpressPlus(3000);
Creating the path /page
as a GET method, that returns Hello, World!
exp.route('/page', 'get', function (req) {
return 'Hello, world!';
});
Getting Data
The req
object returns:
- ip: The user's IP address, as a string.
- params: The URL parameter values, as a JSON object. i.e. if your path is /users/:name, and the user visits /users/hello, params.name would return "hello"
- query: The URL query parameters, as a JSON object.
- body: Returns the JSON body of the request.
- protocol: Returns the protocol. (i.e. https)
- xhr: Returns
true
if the method is using xhr, andfalse
otherwise. - path: Returns the URL path.
- host: Returns the hostname (i.e. example.com)
- headers: A list of the headers, as an array.
exp.parse(content, title, options)
This option would return the provided content
tag as a full HTML page.
Example:
exp.parse(`<h1>Hello, World!</h1>`, 'home', {});
would return:
<!DOCTYPE html>
<html>
<head>
<title>home</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
Options (all optional)
- head: HTML that should appear in the head tag, above the default title tag.
- nav: HTML that should appear at the top of the body tag. Commonly used to display the navigation bar.
- footer: HTML that should appear at the bottom of the body tag.
exp.rd(route, content)
Creates a page that redirects to the route provided, with the message provided.
exp.route('/admin', 'get', function (req) {
if (!admin) return exp.rd("/", "403 — Redirecting...");
});
Routing Example
const ExpressPlus = require('@hellomelo/expressplus');
const exp = new ExpressPlus(3000);
const options = { nav: `<div id="nav"></div>` };
exp.route('/', 'get', function (req) {
return exp.parse(`
<h1>Hello, World!</h1>
<p>This is an ExpressPlus webpage.</p>
`, 'My App', options);
});
Static Files
exp.static('folder');
Restricting Access
exp.restrict("*", function (req) => {
if (req.ip = "blacklisted ip") {
return 'Your IP was banned!'; // display this instead of the page
}else{
return true; // display the actual page
}
});
Ratelimiting
When creating an ExpressPlus instance, the constructor has an extra parameter for ratelimiting.
const exp = new ExpressPlus(3000, 20);
As you can see above, there is a new parameter with the value of 20
. This is the number of requests that can be made on a page in 60 seconds. If not defined, it will be automatically set to 100
.