README
Curie
A modular Node.js http/1.0 framework
Table of Content
Config
Curie-server accepts a number of configuration options of the type:
interface ServerParams {
routes: string
routeParser: ClassConstructor<RouteParser>
public: string
port: number
listeners: [string, string | RegExp]
middleware: [string, string | RegExp]
database: string
preRun: string[]
root: string
}
Keep in mind that all the paths are relative to the main file
File structure
curie-server supports a multi file structure. An example file structure:
|public/
|--css/
|----main.css
|--js/
|----main.js
|routes/
|--index.pug
|listeners/
|--index.list.ts
|middleware/
|--logger.mdw.ts
|index.ts
|database.ts
Main file
The main file is the file, which is responsible for starting your application.
import { initApp } from "curie-server"
initApp({
port: 8000,
public: "../public",
routes: "../routes",
listeners: ["./listeners", "list.[jt]s"],
middleware: ["./middleware", "mdw.[jt]s"],
database: ""
})
Database file
The database file is responcible for connecting with your database. It should export a class which extends the DBridge class. You may create your own class or use the so-called out of the box PotgreSQL DBridge. It should look something like it:
import { PostDBridge, database } from "curie-server"
@database("postgres://postgres:postgres@127.0.0.1:5432/postgres")
export default class extends PostDBridge {}
Listeners
Listeners are responsible for responding to incoming http requests. Both their location and extension are specified in the Server
constructor parameters in the main file. A listener should extend the Listener class, implement onGET
and/or onPOST
method(s), such that they return CallbackReturnType. Example:
import c, { Listener, hookup } from "curie-server";
@hookup("/")
export default class extends Listener {
async onGET(req: c.Request, res: c.Response): c.CallbackReturnType {
this.server.routeParser.render(res, "index")
return [null, false]
}
}
Middleware
Middleware is responcible for interrupting incoming requests and can even reject them. Middleware should extends the Middleware class and return the CallbackReturnType. It should look something like this:
import { CallbackReturnType, Middleware, withTime, Request, Response, c_log, use } from "curie-server";
@use()
export default class extends Middleware {
async intercept(req: Request, res: Response) {
c_log(withTime(`[LOGGER]> ${req.method}: ${req.url || ""}`))
return [null, true] as CallbackReturnType
}
}
RouteParser
The RouteParser
is responsible for parsing and rendering template files. If you want to use the lamplating language of your choice, you should provide a class that extends the RouteParser
.
abstract class RouteParser<RouteType = any> {
path: string
routes: LooseObject<RouteType>
server: Server
public constructor(path: string, server: Server) {/*...*/}
abstract compile(route: string): CallbackReturnType
abstract async compileAll(): Promise<CallbackReturnType>
abstract async render(res: Response, route: string, locals?: LooseObject): Promise<CallbackReturnType>
}
Out of the box curie-server is providing support for the pug templating lang.
Routes
Routes are themplates rendered by the RouteParser. Out of the box you get the PugParser
, which compiles .pug
files and allows you to query items from the database
(template: //#
Middleware is responcible for interrupting incoming requests and can even reject them. Middleware should extends the Middleware class and return the CallbackReturnType. It should look something like this:
import { CallbackReturnType, Middleware, withTime, Request, Response, c_log, use } from "curie-server";
@use()
export default class extends Middleware {
async intercept(req: Request, res: Response) {
c_log(withTime(`[LOGGER]> ${req.method}: ${req.url || ""}`))
return [null, true] as CallbackReturnType
}
}
RouteParser
The RouteParser
is responsible for parsing and rendering template files. If you want to use the lamplating language of your choice, you should provide a class that extends the RouteParser
.
abstract class RouteParser<RouteType = any> {
path: string
routes: LooseObject<RouteType>
server: Server
public constructor(path: string, server: Server) {/*...*/}
abstract compile(route: string): CallbackReturnType
abstract async compileAll(): Promise<CallbackReturnType>
abstract async render(res: Response, route: string, locals?: LooseObject): Promise<CallbackReturnType>
}
Out of the box curie-server is providing support for the pug templating lang.
Routes
Routes are themplates rendered by the RouteParser. Out of the box you get the PugParser
, which compiles .pug
files and allows you to query items from the database
(template: //#