A server framework for easy routing
is a module for easily providing several pluggable RESTful APIs and other
dynamic content on a single server. It extends
npm install -g api
api comes with an executable. If you run
api from your console, the script
walks up the path of your current working directory and looks for a directory
.conf that contains a file named
api.json. This file must contain
several values. Here's an annotated version of such a file.
"protocol": "http" // "http" or "https""connection": // host and port of that server"hostname": "localhost""port": 1337"directories":"modules": ".conf/modules" // directory that contains submodules"modules":"example" // modules that are loaded on startup"errorPages": // error pages"404": ".conf/error/404.html""500": ".conf/error/500.html""logFile": ".conf/api.log" // the log file
Just copy & paste this file to
.conf/api.json, if you want to use it, but
don’t forget to remove the comments. JSON files are not allowed to contain
comments for some reason.
This example configuration starts a server at
http://localhost:1337. It also
.conf/module as the directory for submodules and registers one module
"example". To add code for that module, you have to create the file
.conf/modules/example.js. A module must export a single function
function (app, logger, conf, globalConf, started)).
appis a reference of the Server object.
loggeris a stoopid logger.
confis a module specific configuration object.
globalConfis the global configuration object from
startedis a callback (
function (err)), that is used to return errors or let api know, that the module has been started.
moduleexports =// register request listenersappget'^/hello-world.html'logger.info'Somebody visited "/hello-world.html".';respwriteHead200 'Content-Type': 'text/html' ;respend'<b>Hello World!</b>';;started; // send the callback after everything has been set up.
You can try the example by starting
api from the console. You should see the
following output on
[process] - Server up and running. [process] - Starting module "example". [process] - Module "example" up and running.
Now you can visit
http://localhost:1337/hello-world.html and you should see
as well as
[process] - Somebody visited "/hello-world.html"
Server object has all the methods of
https.Server, if you configured it with https). There are also some additional
methods, that are more or less shortcuts for common events:
on(path, callback)handles any requests that match
path, no matter which http method is used.
get(path, callback)handles all GET request that match
post(path, callback)handles all POST requests that match
put(path, callback)handles all PUT requests that match
delete(path, callback)handles all DELETE requests that match
head(path, callback)handles all HEAD requests that match
path is a string representation of that
/) don’t need to be escaped.
callback is a callback function (
function (req, resp)) just like in the
http 'request' event.
Copyright © 2011-2012 Paul Vorbach
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.