UNPKG

4.8 kBJavaScriptView Raw
1// Copyright 2012 Mark Cavage, Inc. All rights reserved.
2
3'use strict';
4
5var assert = require('assert-plus');
6var errors = require('restify-errors');
7
8var bunyan = require('./bunyan_helper');
9var Router = require('./router');
10var Server = require('./server');
11var shallowCopy = require('./utils').shallowCopy;
12
13var InternalError = errors.InternalError;
14
15/**
16 * A restify server object is the main interface through which you will register
17 * routes and handlers for incoming requests.
18 *
19 * @public
20 * @function createServer
21 * @param {Object} [options] - an options object
22 * @param {String} [options.name="restify"] - Name of the server.
23 * @param {Boolean} [options.dtrace=false] - enable DTrace support
24 * @param {Router} [options.router=new Router(opts)] - Router
25 * @param {Object} [options.log=bunyan.createLogger(options.name || "restify")]
26 * - [bunyan](https://github.com/trentm/node-bunyan) instance.
27 * @param {String} [options.url] - Once listen() is called, this will be filled
28 * in with where the server is running.
29 * @param {String|Buffer} [options.certificate] - If you want to create an HTTPS
30 * server, pass in a PEM-encoded certificate and key.
31 * @param {String|Buffer} [options.key] - If you want to create an HTTPS server,
32 * pass in a PEM-encoded certificate and key.
33 * @param {Object} [options.formatters] - Custom response formatters for
34 * `res.send()`.
35 * @param {Boolean} [options.handleUncaughtExceptions=false] - When true restify
36 * will use a domain to catch and respond to any uncaught
37 * exceptions that occur in it's handler stack.
38 * [bunyan](https://github.com/trentm/node-bunyan) instance.
39 * response header, default is `restify`. Pass empty string to unset the header.
40 * Comes with significant negative performance impact.
41 * @param {Object} [options.spdy] - Any options accepted by
42 * [node-spdy](https://github.com/indutny/node-spdy).
43 * @param {Object} [options.http2] - Any options accepted by
44 * [http2.createSecureServer](https://nodejs.org/api/http2.html).
45 * @param {Boolean} [options.handleUpgrades=false] - Hook the `upgrade` event
46 * from the node HTTP server, pushing `Connection: Upgrade` requests through the
47 * regular request handling chain.
48 * @param {Boolean} [options.onceNext=false] - Prevents calling next multiple
49 * times
50 * @param {Boolean} [options.strictNext=false] - Throws error when next() is
51 * called more than once, enabled onceNext option
52 * @param {Object} [options.httpsServerOptions] - Any options accepted by
53 * [node-https Server](http://nodejs.org/api/https.html#https_https).
54 * If provided the following restify server options will be ignored:
55 * spdy, ca, certificate, key, passphrase, rejectUnauthorized, requestCert and
56 * ciphers; however these can all be specified on httpsServerOptions.
57 * @param {Boolean} [options.noWriteContinue=false] - prevents
58 * `res.writeContinue()` in `server.on('checkContinue')` when proxing
59 * @param {Boolean} [options.ignoreTrailingSlash=false] - ignore trailing slash
60 * on paths
61 * @param {Boolean} [options.strictFormatters=true] - enables strict formatters
62 * behavior: a formatter matching the response's content-type is required. If
63 * not found, the response's content-type is automatically set to
64 * 'application/octet-stream'. If a formatter for that content-type is not
65 * found, sending the response errors.
66 * @example
67 * var restify = require('restify');
68 * var server = restify.createServer();
69 *
70 * server.listen(8080, function () {
71 * console.log('ready on %s', server.url);
72 * });
73 * @returns {Server} server
74 */
75function createServer(options) {
76 assert.optionalObject(options, 'options');
77
78 var opts = shallowCopy(options || {});
79 var server;
80
81 // empty string should override default value.
82 opts.name = opts.hasOwnProperty('name') ? opts.name : 'restify';
83 opts.log = opts.log || bunyan.createLogger(opts.name || 'restify');
84 opts.router = opts.router || new Router(opts);
85
86 server = new Server(opts);
87
88 if (opts.handleUncaughtExceptions) {
89 server.on('uncaughtException', function onUncaughtException(
90 req,
91 res,
92 route,
93 e
94 ) {
95 if (
96 this.listeners('uncaughtException').length > 1 ||
97 res.headersSent
98 ) {
99 return false;
100 }
101
102 res.send(new InternalError(e, e.message || 'unexpected error'));
103 return true;
104 });
105 }
106
107 return server;
108}
109
110///--- Exports
111
112module.exports.bunyan = bunyan;
113module.exports.createServer = createServer;
114module.exports.formatters = require('./formatters');
115module.exports.plugins = require('./plugins');
116module.exports.pre = require('./plugins').pre;
117module.exports.helpers = { compose: require('./helpers/chainComposer') };