titan

A stellar API framework built on top of Argo.

Titan: Stellar APIs

A stellar API framework built on top of Argo. Titan will provide any API management functionality needed by modern API developers.

  1. CORS Management
  2. Compression
  3. Logging
  4. API Resource Definition
  5. Interoperable with Argo Packages

Grab this package off npm using the command.

$ npm install titan

Hello World!

The basic Titan server looks like this.

var titan = require('titan');

titan()
  .use(function(handle) {
    handle('response', function(env, next) {
      env.response.body = {'text': 'Hello World'};
      env.response.statusCode = 200;
      env.response.setHeader('Content-Type', 'application/json');
      next(env);  
    });  
  })
  .listen(3000);

Documentation

### handleFunction(type, [options], callback)
  • type: 'request'or 'response'

  • options: Mostly used for internal purposes. Optional.

  • callback(env, next): A request or response callback. envis an environment context that is passed to every handler, and nextis a reference to the next function in the pipeline.

When the handler is complete and wishes to pass to the next function in the pipeline, it must call next(env).

### use(handleFunction)

handleFunctionis used to set up request and response handlers.

titan()
  //For every request add 'X-Custom-Header' with value 'Yippee!'
  .use(function(handle) {
    handle('request', function(env, next) {
      env.request.headers['X-Custom-Header'] = 'Yippee!';
      next(env);
    });
  })
### use(package)

Alias for include(package).

### route(path, [options], handleFunction)
  • path: a regular expression used to match HTTP Request URI path.

  • options: an object with a methodsproperty to filter HTTP methods (e.g., { methods: ['GET','POST'] }). Optional.

  • handleFunction: Same as in use.

Example:

titan()
  .route('^/greeting$', function(handle) {
    handle('request', function(env, next) {
      env.response.statusCode = 200;
      env.response.headers = { 'Content-Type': 'text/plain' };
      env.response.body = 'Hello World!';

      next(env);
    });
  })

Method filters

Method filter built on top of route. delcorresponds to the DELETE method.

Example:

titan()
  .get('^/puppies$', function(handle) {
    handle('request', function(env, next) {
      env.response.body = JSON.stringify([{name: 'Sparky', breed: 'Fox Terrier' }]);
      next(env);
    });
  })
### map(path, [options], titanSegmentFunction)

mapis used to delegate control to sub-Argo instances based on a request URI path.

  • path: a regular expression used to match the HTTP Request URI path.

  • options: an object with a methodsproperty to filter HTTP methods (e.g., { methods: ['GET','POST'] }). Optional.

  • titanSegmentFunction: a function that is passed an instance of argofor additional setup.

Example:

titan()
  .map('^/payments', function(server) {
    server
      .use(oauth)
      .target('http://backend_payment_server');
  })
### allow([options])

allowenables CORS support for your titan server.

NOTE: To enable CORS for any domain provide an asterisk (*) as the argument to this function.

  • options: an object with:
    • a methodsproperty to filter HTTP methods (e.g., { methods: ['GET','POST'] }).
    • a headersproperty to filter when HTTP Headers are allowed to be included in the request.(e.g. {headers: ['x-tenant-id']}).
    • a maxAgeproperty to indicate how long a preflight request can be cached. (e.g. {maxAge: '43200'}).
    • an originsproperty to filter which domains can access the resource (e.g. { origins: ['http://foo.example'] }).

Example:

titan()
  .allow('*')
titan()
  .allow({
    methods: ['DELETE', 'PUT', 'PATCH'],
    origins: ['http://foo.example'],
    maxAge: '432000',
    headers: ['x-tenant-id']  
  })
### compress()

compressenables gzip compression for your titan server.

Example:

titan()
  .compress()
### logger()

loggerturns on a Common Log Format logger for your titan server.

Example:

titan()
  .logger()
### add(resource, args)

addadds an additional argo-resource package to your titan server. See the argo-resourcerepo for details on creating resources.

var HelloResource = require('./hello_resource');

titan()
  .add(HelloResource);
### load(pathToResources)
  • pathToResource: String path to directory containing argo resource files.

loadloads a directory of argo-resource files without having to specifiy each one individually.

var HelloResource = require('./hello_resource');

titan()
  .load(path.join(__dirname, 'resources'));

Issues and Contributing

Have and issue? Simply file it with github issues. This project has been stable for about a year, but issues do arise from time to time.

Want to contribute? Simply fork the project, make your changes, and file a pull request. We'll review within two to three days of receiving and provide feedback accordingly.

License

Copyright (c) 2014-2015 Apigee and Contributors

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, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Repository

https://github.com/argo/titan


上一篇:zetta
下一篇:argod

相关推荐

  • 如何Appcelerator Titanium移动工作吗?

    Darrell Brogdon(https://stackoverflow.com/users/55589/darrellbrogdon)提出了一个问题:How Does Appcelerator T...

    2 年前
  • vm-titanium

    Implementation of node.js's vm module in Titanium vmtitanium node.js vm(http://nodejs.org/api/vm....

    9 个月前
  • titaniumifier

    Get a Titanium SDK CommonJS module out of a Node package Titaniumifier ============= Build Statu...

    9 个月前
  • titanium-resolve

    resolve which handles titanium field support in package.json titaniumresolve ================ Buil...

    9 个月前
  • titanium-os

    node.jsstyle os for Titanium titaniumos node.jsstyle for Titanium. It can serve as a dropin repl...

    9 个月前
  • titanium-code-processor

    A code processing tool for Titanium Mobile Titanium Code Processor ======================= The Tit...

    1 年前
  • titanium

    Appcelerator Titanium Command line Titanium CLI Titanium CLI(https://github.com/appcelerator/tit...

    1 年前
  • node-titanium-sdk

    Appcelerator Titanium SDK Library TitaniumSDK Greenkeeper badge(https://badges.greenkeeper.io/app...

    1 年前
  • grunt-titaniumifier

    Get a Titanium CommonJS Module out of your Node.js package using Grunt grunttitaniumifier =========...

    9 个月前
  • grunt-titanium

    grunt plugin for titanium CLI grunttitanium NPM version(https://badge.fury.io/js/grunttitanium.png...

    1 年前

官方社区

扫码加入 JavaScript 社区