How to use the plugins function from restify

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

restify.plugins is a module in the restify framework that provides several built-in plugins that can be used for common tasks like logging, throttling, and request/response parsing.

33
34
35
36
37
38
39
40
41

this.server.use(restify.plugins.authorizationParser());
this.server.use(restify.plugins.queryParser());
this.server.use(restify.plugins.gzipResponse());
this.server.use(
    restify.plugins.bodyParser({
        mapParams: true
    })
);
fork icon83
star icon527
watch icon44

+ 5 other calls in file

76
77
78
79
80
81
82
83
84
    restify.plugins.throttle(throttleOptions),
    restify.plugins.dateParser(),
    restify.plugins.queryParser(),
    restify.plugins.fullResponse(),
    restify.plugins.bodyParser(),
    restify.plugins.gzipResponse()
];

server.use(plugins);
fork icon25
star icon30
watch icon7

+ 223 other calls in file

How does restify.plugins work?

restify.plugins is a built-in module in the Restify framework that provides a set of pre-built plugins for handling common tasks such as parsing request headers, handling query parameters, and serving static files. These plugins are middleware functions that can be added to the Restify server and executed in a specific order to perform specific tasks. The restify.plugins module also provides a mechanism for creating custom plugins to handle specific requirements in the application.

188
189
190
191
192
193
194
195
196
197
**例子**

_serveStatic 模块与大多数其他插件不同,因为预期您将它映射到路由上,如下所示:_

```javascript
server.get('/zh-CN/docs/current/*', restify.plugins.serveStatic({
  directory: './documentation/v1',
  default: 'index.html'
}));
```
fork icon0
star icon2
watch icon1

+ 9 other calls in file

21
22
23
24
25
26
27
28
29
30

// res.pipe does not work if Gzip is enabled
//server.use(restify.plugins.gzipResponse());

server.use(
    restify.plugins.queryParser({
        allowDots: true,
        mapParams: true
    })
);
fork icon245
star icon0
watch icon54

+ 5 other calls in file

Ai Example

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

const server = restify.createServer();

// Add request logging
server.use(restify.plugins.requestLogger());

// Parse query parameters
server.use(restify.plugins.queryParser());

// Define a route
server.get("/hello", (req, res, next) => {
  res.send("Hello, World!");
  return next();
});

// Start the server
server.listen(8080, () => {
  console.log(`Server listening on port ${server.address().port}`);
});

In this example, restify.plugins is used to add middleware to the server that logs incoming requests and parses query parameters. This allows the server to respond appropriately to requests for the /hello route, sending back a simple greeting.

116
117
118
119
120
121
122
123
124
server.use(function(req, res, next) {
  req.log.info({req: req}, 'REQUEST');
  next();
});

server.on('after', restify.plugins.auditLogger({
  log: server.log,
  event: 'after',
}));
fork icon25
star icon77
watch icon28

+ 3 other calls in file

91
92
93
94
95
96
97
98
99
100
  res.end();
});
server.get("/docs", (req, res, next) => res.redirect(`${packageJson.basePath}docs/index.html`, next));
server.get(
  "/docs/*",
  restify.plugins.serveStatic({
    appendRequestPath: false,
    directory: pathToSwaggerUi,
    default: "index.html",
  })
fork icon6
star icon2
watch icon0

110
111
112
113
114
115
116
117
118
119
const server = wrapper.wrapServer(restifyServer, _registry);

// Apply the requestLogger, unless set to false!
if (options.transom && options.transom.requestLogger !== false) {
  server.use(
    restify.plugins.requestLogger({
      log: server.log,
    })
  );
}
fork icon0
star icon10
watch icon0

+ 2 other calls in file

167
168
169
170
171
172
173
174
175
176
177
178
179
  clients.createJsonClient(req.params.uri); // test: source, ssrfSink


  next();
})


server.get('/hello2/:name', restify.plugins.conditionalHandler([ // test: setup
  { version: ['2.0.0', '2.1.0', '2.2.0'], handler: sendV2 }
]));


server.get('/version/test', restify.plugins.conditionalHandler([ //test: setup
fork icon0
star icon2
watch icon0

+ 3 other calls in file

34
35
36
37
38
39
40
41
42
43
44
45




server.get({
    path: '/todo/*',
    name: 'todoapi'
    }, restify.plugins.serveStatic({
    'directory': __dirname,
    'default': 'index.html'
}));

fork icon0
star icon1
watch icon0

127
128
129
130
131
132
133
134
135
136
137
138


process.on('uncaughtException', (err) => {
  logError(err, { message: 'Uncaught exception' })
})


const restifyAuthorizationParser = restify.plugins.authorizationParser()
const stelaceTooling = {
  // utility functions often used
  logError,
  createError,
fork icon0
star icon0
watch icon0

+ 5 other calls in file

186
187
188
189
190
191
192
193
194
195
196
197
198
199




    // static files: /, /index.html, /images...
    //var STATIS_FILE_RE = /\/?\.css|\/?\.js|\/?\.png|\/?\.jpg|\/?\.gif|\/?\.jpeg|\/?\.less|\/?\.eot|\/?\.svg|\/?\.ttf|\/?\.otf|\/?\.woff|\/?\.pdf|\/?\.ico|\/?\.json|\/?\.wav|\/?\.mp3/;
    var STATIS_FILE_RE = /\.(css|js|jpe?g|png|gif|less|eot|svg|bmp|tiff|ttf|otf|woff|pdf|ico|json|wav|ogg|mp3?|xml|woff2|map)$/i;
    server.get(STATIS_FILE_RE, restify.plugins.serveStatic({ directory: './public/docs', default: 'index.html', maxAge: 0 }));
//    server.get(/^\/((.*)(\.)(.+))*$/, restify.serveStatic({ directory: './TruMenuWeb', default: "index.html" }));





fork icon0
star icon0
watch icon0

44
45
46
47
48
49
50
51
52
53
  );
});

this.server.get(
  "/public/*", // don't forget the `/*`
  restify.plugins.serveStaticFiles("./bin/public")
); // GET /public/index.html -> ./doc/v1/index.html file


// ADMIN ACTIONS
fork icon0
star icon0
watch icon0

328
329
330
331
332
333
334
335
336
337
// Set a per request pino logger (with requestid filled in)
server.use(restify.plugins.requestLogger());

// Allow 5 requests/second by IP, and burst to 10
server.use(
    restify.plugins.throttle({
        burst: 10,
        rate: 5,
        ip: true
    })
fork icon0
star icon0
watch icon0

+ 3 other calls in file

54
55
56
57
58
59
60
61
62
63
server.use(restify.plugins.authorizationParser());
server.use(restify.plugins.queryParser());
server.use(restify.plugins.gzipResponse());

server.get('/docs/*', // don't forget the `/*`
    restify.plugins.serveStaticFiles('./public/docs')
);


/**
fork icon0
star icon0
watch icon0

99
100
101
102
103
104
105
106
107
108
  restify.plugins.acceptParser(app.acceptable),
  restify.plugins.authorizationParser(),
  restify.plugins.dateParser(),
  restify.plugins.queryParser(),
  restify.plugins.jsonp(),
  restify.plugins.requestLogger(),
  restify.plugins.fullResponse(),
  CookieParser.parse
];

fork icon0
star icon0
watch icon0

+ 6 other calls in file

307
308
309
310
311
312
313
314
315
316

The serveStatic module is different than most of the other plugins, in that it
is expected that you are going to map it to a route, as below:

```js
server.get(/\/docs\/current\/?.*/, restify.plugins.serveStatic({
  directory: './documentation/v1',
  default: 'index.html'
}));
```
fork icon0
star icon0
watch icon1

+ 3 other calls in file

194
195
196
197
198
199
200
201
202
203

// Put any routing, response, etc. logic here.
function setupCommonRestifyConfigAndRoutes(server) {
  // limit responses to only requests for acceptable types
  server.pre(
    restify.plugins.acceptParser([
      'application/json',
      'application/javascript',
      'text/html',
      'application/octet-stream',
fork icon0
star icon0
watch icon5

105
106
107
108
109
110
111
112
113
114
  './handlers/inventoryCount',
  './handlers/product'
])

// serve client as static files
server.get('/*', restify.plugins.serveStatic({
  directory: './client/build',
  default: 'index.html',
  appendRequestPath: true
}))
fork icon0
star icon0
watch icon3