UNPKG

4.62 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 pino = require('pino');
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=pino({name:options.name || "restify"})]
26 * - [pino](https://github.com/pinojs/pino) 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 its handler stack.
38 * Comes with significant negative performance impact.
39 * @param {Object} [options.spdy] - Any options accepted by
40 * [node-spdy](https://github.com/indutny/node-spdy).
41 * @param {Object} [options.http2] - Any options accepted by
42 * [http2.createSecureServer](https://nodejs.org/api/http2.html).
43 * @param {Boolean} [options.handleUpgrades=false] - Hook the `upgrade` event
44 * from the node HTTP server, pushing `Connection: Upgrade` requests through the
45 * regular request handling chain.
46 * @param {Boolean} [options.onceNext=false] - Prevents calling next multiple
47 * times
48 * @param {Boolean} [options.strictNext=false] - Throws error when next() is
49 * called more than once, enabled onceNext option
50 * @param {Object} [options.httpsServerOptions] - Any options accepted by
51 * [node-https Server](http://nodejs.org/api/https.html#https_https).
52 * If provided the following restify server options will be ignored:
53 * spdy, ca, certificate, key, passphrase, rejectUnauthorized, requestCert and
54 * ciphers; however these can all be specified on httpsServerOptions.
55 * @param {Boolean} [options.noWriteContinue=false] - prevents
56 * `res.writeContinue()` in `server.on('checkContinue')` when proxing
57 * @param {Boolean} [options.ignoreTrailingSlash=false] - ignore trailing slash
58 * on paths
59 * @param {Boolean} [options.strictFormatters=true] - enables strict formatters
60 * behavior: a formatter matching the response's content-type is required. If
61 * not found, the response's content-type is automatically set to
62 * 'application/octet-stream'. If a formatter for that content-type is not
63 * found, sending the response errors.
64 * @example
65 * var restify = require('restify');
66 * var server = restify.createServer();
67 *
68 * server.listen(8080, function () {
69 * console.log('ready on %s', server.url);
70 * });
71 * @returns {Server} server
72 */
73function createServer(options) {
74 assert.optionalObject(options, 'options');
75
76 var opts = shallowCopy(options || {});
77 var server;
78
79 // empty string should override default value.
80 opts.name = opts.hasOwnProperty('name') ? opts.name : 'restify';
81 opts.log = opts.log || pino({ name: opts.name || 'restify' });
82 opts.router = opts.router || new Router(opts);
83
84 server = new Server(opts);
85
86 if (opts.handleUncaughtExceptions) {
87 server.on('uncaughtException', function onUncaughtException(
88 req,
89 res,
90 route,
91 e
92 ) {
93 if (
94 this.listeners('uncaughtException').length > 1 ||
95 res.headersSent
96 ) {
97 return false;
98 }
99
100 res.send(new InternalError(e, e.message || 'unexpected error'));
101 return true;
102 });
103 }
104
105 return server;
106}
107
108///--- Exports
109
110module.exports.logger = pino;
111module.exports.createServer = createServer;
112module.exports.formatters = require('./formatters');
113module.exports.plugins = require('./plugins');
114module.exports.pre = require('./plugins').pre;
115module.exports.helpers = { compose: require('./helpers/chainComposer') };