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:
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:
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:
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.
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: //# $<variable_name>: <query>).
//# $posts: SELECT * FROM posts
<!DOCTYPE html>
html(lang="en")
head
// ...
body
ul.posts
for post in posts
li.posts__post
h2.posts__post__title= post.title
p.posts__post__body= post.body
Single file approach
While I highly advise you to take the advatnage of the multi file structure, around which the curie-server was built, you can fit everything into a single file.
CallbackReturnType is a value returned by many curie-server functions, but is used especially in the Listener and the Middleware classes. The first part of the tuple is the Error, and the 2nd one is the ShouldContinue boolean, which tells the inner loop whether is should send the Response to the client or continue.
typeCallbackReturnType=[Error|null,boolean]
Helpers
Logging
curie-server comes with a plethora of tools to help you log info.
initApp accepts a parameter called preRun. It's simply an array of commands to run before the rest of initialization (loading files, setting up events etc.). Errors won't terminate the main process, but only propagate the error message to the console (same with stdout).
initApp({
// ...
preRun:[
"tsc"
]
})
In the example, the tsc command will run before loading files to a memory, thus allowing you to compile i.e. typescript files.
CLI
curie-server listens for a console input and evaluates it upon pressing Enter. Errors won't terminate the main process. this refers to the Server instance.