Creates an instance of WebfocusApp.
The configuration
object is optional and extends the values:
{
port: 0,
name: "Default Application Name",
dirname: path.join(__dirname, 'views'),
static: path.join(__dirname, 'static'),
components: []
}
-
If an invalid
port
is given it is changed to 0. Usingport == 0
will make the app listen to any available port. You can check the port after starting the server:app.start().address().port
. -
The value of
components
is always replaced by an empty array. It is later be filled with theurlname
of the components regitered. -
dirname
is the location used byexpress
to renderpug
files. For more information on customizing the PUG Files see Section Custom PUG Files. -
static
is the location used byexpress
to send static files.
The constructor registers two handlers for http GET requests to:
-
/
will use pug to render the file atlayouts/index.pug
. -
/api/
will return a json list with theurlname
s registered in this application. It enablesapplication/json
andapplication/x-www-form-urlencoded
http communication for subpaths of/api/
(access toreq.body
on the component middleware). To upload files (multipart/form-data
) you can usemutler
or any other implementation on specific comonents.
Returns false if the application has already started or if there exists an component with the same urlname registered.
Registers he following handlers:
/api/${component.urlname}
to use thecomponent.app
middleware./${component.urlname}
to use thecomponent.staticApp
middleware and render pug files withincomponent.dirname
. It usually try to serve files staticaly fromcomponent.dirname
.
Example, a component with the following structure
component/
index.js
file.txt
file.pug
index.pug
subfolder/
index.pug
file.pug
file.txt
Responds the following:
GET /component/ -> pug renders component/index.pug
GET /component/file.txt -> statically return component/file.txt
GET /component/file -> pug renders component/file.pug
GET /component/anotherfile.txt -> pug renders component/index.pug
GET /component/subfolder/ -> pug renders component/subfolder/index.pug
GET /component/subfolder/file -> pug renders component/subfolder/file.pug
GET /component/subfolder/file.txt -> statically return component/subfolder/file.txt
GET /component/subfolder/anotherfile.txt -> pug renders component/index.pug
GET /component/anothersubfolder/anotherfile.txt -> pug renders component/index.pug
GET /api/component/ -> check the app component middleware
Gets an WebfocusComponent
given its urlname or null if it is not registered.
Gets all WebfocusComponent
s urlnames registered.
Emits name
with ...obj
to all registered components.
Starts the server listening to the port specified in configuration.
It defines the final handlers for requests if there was an error or there was no handler for the request.
Method | Request Path | Response Status | Sends |
---|---|---|---|
Any | /api/* |
404 or 500 |
JSON: { error: string, stack?: string } |
Any | * |
200 | Files from configuration.static folder |
GET | * |
404 | Render of layouts/error.pug |
Any | * |
400 or 500 | Render of layouts/error.pug |
Used internally when rendering with pug.
Creates an object to be used by pug when rendering any file.
It adds the the keys configuration
and getComponent
to the object recieved.
-
configuration
is the webfocusApp configuration. -
getComponent(urlname : String) : {urlname: string, name: string, description: string}
(From a theWebfocusComponent
)
Any render will have the req
object, representing the express request.
Error renders (layouts/error.pug
) will also have a error: string
and optionaly a stack: string
.
Component specific renders will have the following keys:
apibaseurl = "/api/${component.urlname}/"
componentbaseurl = "/${component.urlname}/"
component = component
(The specificWebfocusComponent
instance)basedir = webfocusApp.configuration.dirname
(To enable component toextends /layouts/main.pug
)
This framework requires you to create a folder layouts
with the files: index.pug
, error.pug
and main.pug
if you change the default dirname
value for a custom HTML.
Specifically main.pug
will be extended by the components registered to the application, this means that the file must define a space for be extended with block main
and block head
.
To mantain a consistent look through the pages you could also make index.pug and error.pug extend from main.pug (just like any other component)