Transom-core is a foundation for low-code REST API and realtime server development. Transom applications use metadata to configure pre-built and tested modules that allow developers to quickly piece together services that 'just work'!
Transom uses Restify as it's core. We use Passport for authentication, Mongoose for data, SocketIO for realtime updates, Nodemailer for sending emails created with EJS templates!
Did we miss your favorite project or something you need for your product? Create a Transom module and let us know about it!
A transom server without modules, is just an empty Restify server. Adding functionality goes quickly with well thought out modules to provide common REST API functions. Features not available in an existing module can be added directly to the (Restify) server as custom routes or rolled into a custom module that can be loaded along-side with other Transom modules.
$ npm install --save @transomjs/transom-core
An API definition is quite simply a JavaScript Object. It can be a single file, but it doesn't need to be. Break it up into logical pieces as needed or as your project grows.
- Checkout the API definition file from our transom-mongoose-example app.
The following simple example is the index.js
file from a REST API built with Transom.
- Import the Transom-core and create a new instance.
- Import and configure any Transom modules.
- Import your API definition; This is the metadata that defines your API.
- Call transom.initialize() with your metadata object. It will return a Promise that resolves to your Restify server.
- Call server.listen()
// Require your API definition
const myApi = require('./myApi');
// Require the Transom-core and any Transom modules
const Transom = require('@transomjs/transom-core');
const transomMongoose = require('@transomjs/transom-mongoose');
// Instantiate Transom
const transom = new Transom();
// Register and configure Transom modules
transom.configure(transomMongoose, {
mongodb_uri: 'mongodb://localhost/transom-dev'
});
// Initialize all modules at once, returning a Restify server instance.
transom.initialize(myApi).then(function(server){
// Add any additional routes as necessary.
server.get('/hello', function (req, res, next) {
res.json({hello: 'world'});
next();
});
// Add your own Error handlers
// Start the server!
server.listen(7000, function () {
console.log('%s listening at %s', server.name, server.url);
});
});
We've created a few small apis to demonstrate the usage of individual plugins:
- https://github.com/binaryops-wiebo/transom-scaffold-example
- https://github.com/4umfreak/transom-smtp-example
- https://github.com/4umfreak/transom-mongoose-example
- https://github.com/binaryops-wiebo/transom-functions-simple-example
- https://github.com/binaryops-wiebo/transom-functions-secured-example
- https://github.com/binaryops-wiebo/transom-socketio-internal-example
That's easy too. Simply create your own server instance and pass it to Transom after it's been initilized.
// Create your own Restify server instance and initialize it as needed.
const server = restify.createServer();
server.use(myCustomPlugin);
// Later, initialize the registered Transom modules.
transom.initialize(server, myApi);
If you can create simple JavaScript Objects, you can handle the metadata. By using JavaScript Objects, we can piece together bits of metadata from just about anywhere.
TransomCore uses attributes from the transom
node in the definition file to apply defaults on start-up. If you don't need to make any specific configuration changes, the node can be dropped altogether to use the provided defaults.
const myApi = {
transom: {
requestLogger: {},
cors: {
origins: ['http://localhost:8080', 'http://my-dev-server']
},
bodyParser: {
mapParams: true
},
queryParser: {
mapParams: true
},
cookieParser: {},
urlEncodedBodyParser: {
mapParams: true
},
gzipResponse: {},
fullResponse: {},
favicon: {
path: "/assets/favicon.ico"
}
}
};
The following plugins come standard in a Transom based server because we've found them to be both necessary and useful. Options provided in the definition are passed directly to each plugin unless otherwise documented below. See the documentation on each respective plugin as it's going to be more current than if we copied it here.
If you would prefer not to use any of the individual plugins applied in core, set the corresponding option to false. The following config disables the default favicon
plugin.
const myApi = {
transom: {
favicon: false
}
};
http://restify.com/docs/plugins-api/#requestlogger
The requestLogger
option can be used a number of ways depending on your need to customize logging within your API.
- Disable request logging by setting
requestLogger
to false. TransomJS will defer to Restify's default logger but log output will not include a uniquereq_id
on each individual request.
transom: {
requestLogger: false
}
- Create your own Bunyan logger object and pass it in as
log
. Restify will use your logger and include a uniquereq_id
on each individual request.
transom: {
requestLogger: {
log: myCustomBunyan
}
}
- Define a custom Bunyan logger, Restify will create a new Bunyan logger using your configuration.
transom: {
requestLogger: {
name: 'MyTestLogger',
streams: [
{
stream: process.stdout,
level: "debug"
}
]
}
}
https://www.npmjs.com/package/restify-cors-middleware2
The authorization
header is added to the allowHeaders
option automatically as it's required for Bearer authentication.
The origins
option is set to a wildcard for easier development and can be set with an environment variable when moving to test or production. Both preflight & actual middleware are applied. Keep in mind that http://localhost:8100
is different than http://127.0.0.1:8100
which is again different than http://[::]:8100
even though they may all resolve to the same service. CORs middleware can accept an array of String, or regEx to match acceptable URI patters. See the official documentation for additional details.
http://restify.com/docs/plugins-api/#bodyparser
The mapParams
option is set to true by default. Many Transom modules will only look for submitted values in req.params, rather than having to check in each of req.query or req.body.
A child plugin of the bodyParser. The mapParams
option is set to true by default.
http://restify.com/docs/plugins-api/#queryparser The mapParams
option is set to true by default.
https://www.npmjs.com/package/restify-cookies Looking for cookies named access_token
by default.
http://restify.com/docs/plugins-api/#gzipresponse
http://restify.com/docs/plugins-api/#fullresponse
https://www.npmjs.com/package/serve-favicon
If a path
option is not provided, an icon will be served from ./node_modules/transom-core/images/favicon.ico.
Transom modules can be simple middleware, or complex ORM solutions. The following demonstrates how simple a Transom module can really be.
function TransomConsole() {
this.initialize = function(server, options) {
console.log("Initializing Transom-console.");
server.use(function(req, res, next) {
console.log("Transom-console...", req.url);
next();
});
}
}
module.exports = new TransomConsole();
TransomJS is developed and maintained by BinaryOps Software Inc. in Canada.