Package Exports
- api
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
api
is a module for easily providing several pluggable RESTful APIs and other
dynamic content on a single server. It extends
http.Server
.
Installation
npm install -g api
Usage
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
named .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
defines .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)
).
app
is a reference of the Server object.logger
is a stoopid logger.conf
is a module specific configuration object.globalConf
is the global configuration object from.conf/api.json
.started
is a callback (function (err)
), that is used to return errors or let api know, that the module has been started.
Each module may have its own configuration JSON files. These go directly into
the .conf
directory. Just create a JSON file with the name of the module
(e.g. .conf/example.json
) and it will be exposed to the conf
parameter.
Here’s the code of an example module.
.conf/modules/example.js
:
module.exports = function example(app, logger, conf, globalConf, started) {
// register request listeners
app.get('^/hello-world.html', function helloWorld(req, resp) {
logger.info('Somebody visited "/hello-world.html".');
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.end('<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 stdout
:
[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
Hello World!
as well as
[process] - Somebody visited "/hello-world.html"
API
The api Server
object has all the methods of
http.Server
(or
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 matchpath
, no matter which http method is used.get(path, callback)
handles all GET request that matchpath
.post(path, callback)
handles all POST requests that matchpath
.put(path, callback)
handles all PUT requests that matchpath
.delete(path, callback)
handles all DELETE requests that matchpath
.head(path, callback)
handles all HEAD requests that matchpath
.
path
is a string representation of that RegExp
object.
Slashes (/
) don’t need to be escaped.
callback
is a callback function (function (req, resp)
) just like in the
http 'request' event.
Bugs and Issues
If you encounter any bugs or issues, feel free to open an issue at github or send me an e-mail to paul(at)vorb.de.
License
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.