How to use the createServer function from restify

Find comprehensive JavaScript restify.createServer code examples handpicked from public code repositorys.

restify.createServer is a function that creates a new instance of a RESTful web server that can be used to handle HTTP requests and responses.

27
28
29
30
31
32
33
34
35
36
setQueue(queue) {
    this.queue = this.maildrop.queue = queue;
}

createServer() {
    this.server = restify.createServer();

    this.server.use(restify.plugins.authorizationParser());
    this.server.use(restify.plugins.queryParser());
    this.server.use(restify.plugins.gzipResponse());
fork icon83
star icon527
watch icon44

+ 5 other calls in file

72
73
74
75
76
77
78
79
80
81
    });
    metricsManager.createRestifyMetrics();
    opts.backend.opts.metricsManager = metricsManager;
}

var server = restify.createServer(opts.api);

var Backend = require(opts.backend.module);
opts.backend.opts.log = log;
var backend = Backend(opts.backend.opts);
fork icon71
star icon449
watch icon63

+ 3 other calls in file

How does restify.createServer work?

restify.createServer is a function provided by the restify library that creates a new instance of a RESTful web server in Node.js.

When restify.createServer is called, it creates a new Server object that can be used to handle incoming HTTP requests and send HTTP responses. This server object can be configured with various options, such as the port to listen on, SSL settings, and more.

Once the server has been created and configured, it can be started by calling its listen method, which begins listening for incoming requests on the specified port or socket. The server object also includes various methods and properties for handling different types of HTTP requests, such as get, post, put, and del for handling HTTP GET, POST, PUT, and DELETE requests, respectively.

In addition to these basic features, restify.createServer also provides a number of additional capabilities for building robust and scalable RESTful web services, such as support for middleware, routing, authentication, and more.

Overall, restify.createServer provides a powerful and flexible tool for building RESTful web services in Node.js, making it a popular choice for many developers and organizations.

44
45
46
47
48
49
50
51
52
53
##### 清单 3\. 使用 restify 创建 Hello World 应用

```
const restify = require('restify');

const server = restify.createServer();

server.get('/hello', (req, res, next) => {
res.send('Hello World');
return next();
fork icon165
star icon331
watch icon10

+ 11 other calls in file

-4
fork icon65
star icon121
watch icon28

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const restify = require("restify");

// Create a new RESTful web server
const server = restify.createServer({
  name: "My RESTful Web Server",
});

// Define a route to handle HTTP GET requests to '/'
server.get("/", (req, res, next) => {
  res.send("Hello, world!");
  return next();
});

// Start the server and listen on port 8080
server.listen(8080, () => {
  console.log(`${server.name} is listening at ${server.url}`);
});

In this example, we first import the restify library and create a new RESTful web server using restify.createServer, passing in an options object that specifies the name of the server. We then define a route to handle HTTP GET requests to the root path '/'. When a request is received at this route, the server sends a simple response message of "Hello, world!" back to the client. Finally, we start the server by calling its listen method and specifying the port to listen on. Once the server is started, it will listen for incoming HTTP requests and respond appropriately. Overall, this example demonstrates the basic functionality of restify.createServer and shows how it can be used to create a simple RESTful web server in Node.js.

-2
fork icon60
star icon114
watch icon16

+ 15 other calls in file

94
95
96
97
98
99
100
101
102
103
  token: this.config.api.token,
  buildUrl: this.config.buildUrl,
  log: this.log,
});

var server = restify.createServer({
  name: config.name,
  version: require('../package').version,
  log: this.log.child({component: 'http'}),
});
fork icon25
star icon77
watch icon28

+ 3 other calls in file

7
8
9
10
11
12
13
14
15
16
    if (file.indexOf('.js') != -1) {
        controllers[file.split('.')[0]] = require(controllers_path + '/' + file)
    }
})

var server = restify.createServer();

server
    .use(restify.fullResponse())
    .use(restify.bodyParser())
fork icon37
star icon20
watch icon9

+ 11 other calls in file

180
181
182
183
184
185
186
187
188
189
if (options.fluentd_host) {
    addFluentdHost(log, options.fluentd_host);
}

// Init VmapiApp server
this.server = restify.createServer({
    name: 'VMAPI/' + getVersion(),
    log: log.child({ component: 'api' }, true),
    version: apiVersion,
    serverName: 'SmartDataCenter',
fork icon20
star icon11
watch icon0

23
24
25
26
27
28
29
30
31

var app_name = self.config.app_name || 'MyApi';

var uncaught_exception_handler = new UncaughtExceptionHandler({app_name: app_name});

self.server = restify.createServer({
    name: app_name,
    log: logger
});
fork icon14
star icon52
watch icon7

+ 3 other calls in file

2
3
4
5
6
7
8
9
10
11
module.exports = {
  get    : function() {
    return restify;
  },
  create : function() {
    var server = restify.createServer.apply(restify, arguments);

    server.use(restify.acceptParser(server.acceptable));
    server.use(restify.authorizationParser());
    server.use(restify.dateParser());
fork icon9
star icon35
watch icon11

+ 9 other calls in file

44
45
46
47
48
49
50
51
52
53
```js
var restify = require('restify')
  , Respectify = require('respectify')
 
// Create the restify server
var server = restify.createServer()
 
// Create the respectify instance with the new server
var respect = new Respectify(server)
 
fork icon8
star icon15
watch icon12

+ 3 other calls in file

99
100
101
102
103
104
105
106
107
108
},
{
  _server: null,
  name: 'restify',
  start(done) {
    this._server = restify.createServer();
    this._server.use(restify.plugins.queryParser());
    serverHealth.exposeHealthEndpoint(this._server);
    this._server.listen(8080, done);
  },
fork icon4
star icon12
watch icon0

+ 5 other calls in file

52
53
54
55
56
57
58
59
60
61
console.log('Starting up service...')

var map = new MAP(DB_CONNECTOR, attributes);
var api = new API(DB_CONNECTOR, attributes, timestamps);

var server = RESTIFY.createServer({
        name: 'OSMatrix'
});

server.use(RESTIFY.bodyParser({ mapParams: false }));
fork icon3
star icon5
watch icon9

+ 11 other calls in file

141
142
143
144
145
146
147
148
149
		}
		: {})
};

// create the server using a custom formatter
const server = restify.createServer(serverOptions);

// only allow application/json
server.pre(catapultRestifyPlugins.body());
fork icon0
star icon3
watch icon0

10
11
12
13
14
15
16
17
18
19
20
        return '<html><body>' + body + req.params.name + '</body></html>'; // test: source, stackTraceExposureSink, !xssSink, !xss
      }
    }
  }
}
const _server = restify.createServer(opts)


const server = restify.createServer({
  formatters: {
    'text/html': function(req, res, body) { // test: formatter
fork icon0
star icon2
watch icon0

+ 3 other calls in file

-1
fork icon35
star icon52
watch icon5

+ 9 other calls in file

147
148
149
150
151
152
153
154
155
156

我们看到用restify创建REST服务非常的容易,接下来我们将深入学习restify的API。

## 4. restify服务端API

* restify.createServer(): 创建服务器
* server.use(): 注册handler
* server.get(): 路由控制
* server.formatters: 设置content-type
* 异常处理
fork icon18
star icon42
watch icon7

+ 95 other calls in file

5
6
7
8
9
10
11
12
13
var restify = require('restify');
var JSIndexController=require('./jsindexcontroller').JSIndexController;


//JSCommonComplier.JSComplier.SetDomain('http://100.114.65.219');
var server = restify.createServer({name:'HQChart.testserver'});
server.use(restify.plugins.bodyParser());   //支持json post
server.use(restify.plugins.fullResponse()); //跨域
server.use(restify.plugins.gzipResponse()); //支持压缩
fork icon654
star icon4
watch icon0

4
5
6
7
8
9
10
11
12
13

module.exports = function(config) {
  ////////////////////////////////////////////////////////////
  // Create and boot the API Server (if enabled)

  const apiServer = restify.createServer({
    name: 'dns-proxy API Server'
  });

  apiServer.use(restify.pre.sanitizePath());
fork icon42
star icon0
watch icon34