0.0.3 • Published 2 years ago
nodable v0.0.3
Nodable
A compact, high-performance and full-featured web framework based on nodejs. The features include:
- Application context
- Middlewares like Koa.js
- Built-in static service
- Built-in routing service
- Built-in template engine
Installation
npm i nodableGet Started
const app = require("nodable");
app
.serve("/assets/*")
.engine({
root: "template",
imports: { globalName: "" },
})
.on("error", (err, ctx) => {
console.error(err);
ctx.body = {
status: ctx.status,
message: ctx.body,
};
})
.use(async (ctx, next) => {
ctx.custom = "xxxx";
await next();
})
.get("/", async (ctx) => {
console.log(ctx.query);
console.log(ctx.params);
})
.listen();API Reference
Methods
app.serve(path)Serve static resources with the givenpath. It's the syntactic sugar forgetrouting, so thepathmust starts with "/".app.engine(options)Enable template engine with options{ root, imports }.app.on("error", function)Custom unified error handling.app.use(function)Add a middleware like koa.js.app.get(path, [tmpl,] function)Add dynamic route includingpost,put,deleteand other standard request methods, it will auto-render template iftmplparameter exists.app.listen([port])Create and start an application server on the specified port.app.callback()Return a request handler for node's native http server.
Context
Properties
ctx.paramsGet params in route path, wildcard supportsctx.queryGet params in query stringctx.methodGet request methodctx.pathGet request pathctx.urlGet request full hrefctx.protocolGet request protocolctx.hostGet request hostctx.hostnameGet request hostnamectx.originGet request originctx.headersGet headers objectctx.cookiesGet cookies objectctx.statusGet response status codectx.bodyGet response bodyctx.requestGet native requestctx.responseGet native responsectx.status=Set response status codectx.body=Set response body
Methods
ctx.get(name)Get request headers by namectx.set(name, value)Set response headersctx.cookie(name, value[, options])Set cookiesasync ctx.json()Get request body in jsonasync ctx.text()Get request body in textasync ctx.buffer()Get request body in bufferctx.redirect(url[, status])Redirect url with status default 301ctx.view(path, data)Render template file, only if engine enabled.ctx.render(path, data)Render template text, only if engine enabled.ctx.throw(message, status)Throw an error with status code
Route Syntax
/staticstatic route/*Wildcard route, it will returnwildcardvariable inctx.params/:user/:user?/:user(\\d+)
Template Syntax
{{ }}Evaluate code snippet in javascript. Note that the variables do not need to be declared. ex.{{ result = 60*60; }}{{= }}Interpolation. ex.{{= username }}{{? }} {{?? }} {{? }}Conditional statement. ex.
{{? gender == 0 }}
<div>Female</div>
{{?? gender == 1 }}
<div>Male</div>
{{?? }}
<div>Unknown</div>
{{? }}{{~ }} {{~ }}Iterative statement. ex.
<ul>
{{~ users:user:index }}
<li>{{= index+1 }} - {{= user.name }}<li>
{{~ }}
</ul>{{> }}Block placeholder.{{< }}Block content definition.
{{> content }}
{{< content }}
<h1>Hello.</h1>
{{< }}{{@ }}Partial including in layout mode. You must be rendered byview(file, data)method.
// index.html
{{@ header.html }}
// header.html
<h1>Hello.</h1>