HTTP Host
Intro
Anvil's core has a built in HTTP server that is activated with configuration or a command line option:
anvil --host
or
{
"host": true
}
The primary role of the HTTP server is to host static resources. When combined with CI mode, the browser will automatically refresh the page after changes to any source files trigger a build (this happens via socket.io). The HTTP server is started as soon as Anvil completes configuration of all the loaded extensions.
Browser
Anvil will automatically launch a browser tab in your default browser pointed to the root of the site at the correct port if you provide the browser argument:
anvil --host --browser
Configuration
Static routing and port are the two options available for configuration:
{
"port": 3080, // default port
"httpPaths": {
"/" : "./lib" // default route
}
}
Each key in the httpPaths object is the url path and the value is the file path relative to the project's root.
API
The following API calls are available off the anvil.http namespace:
.registerPath( url, filePath )
Register static files to serve at a specific relative url.
.registerRoute( url, verb, callback )
Provide a handler for the url and verb of the form: function( request, response ).
.registerTopic( topic, callback )
Provide a handler for the socket.io topic of the form: function( data, client ) where data is the envelope and client is a handle to the client socket.
.addMiddleware( middleware )
Provide a function that can change the result of a call returned to the browser. Middleware is a function of the form: function( body, req, res ) where body is the complete body of the request, and req and res are the original request and response objects provided by express.
.addCompiler( extension, mimeType, compiler )
Provide a function that can translate from one source format to another. Extension is the extension (including the leading period, '.coffee') of the original requested file that the compiler can process. MimeType is the resulting format ( 'text/javascript' ). Compiler is a function of the form: function( content, done ) where content is the original source and the done argument is a callback of the form function( result, error ).