Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/index.restdown
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,16 @@ which is just a `RegExp` to check against the requested file name. Lastly, you
can pass in a `maxAge` numeric, which will set the `Cache-Control` header.
Default is `3600` (1 hour).

An additional option for serving a static file is to pass `file` in to the
serveStatic method as an option. The following will serve index.html from
the documentation/v1/ directory anytime a client requests `/home/`.

server.get(/\/home\//, restify.serveStatic({
directory: './documentation/v1',
file: 'index.html'
}));


### Throttle

restify ships with a fairly comprehensive implementation of
Expand Down
13 changes: 11 additions & 2 deletions lib/plugins/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function serveStatic(opts) {
assert.optionalNumber(opts.maxAge, 'options.maxAge');
assert.optionalObject(opts.match, 'options.match');
assert.optionalString(opts.charSet, 'options.charSet');
assert.optionalString(opts.file, 'options.file');

var p = path.normalize(opts.directory).replace(/\\/g, '/');
var re = new RegExp('^' + escapeRE(p) + '/?.*');
Expand Down Expand Up @@ -97,8 +98,16 @@ function serveStatic(opts) {
}

function serve(req, res, next) {
var file = path.join(opts.directory,
decodeURIComponent(req.path()));
var file;

if (opts.file) {
//serves a direct file
file = path.join(opts.directory,
decodeURIComponent(opts.file));
} else {
file = path.join(opts.directory,
decodeURIComponent(req.path()));
}

if (req.method !== 'GET' && req.method !== 'HEAD') {
next(new MethodNotAllowedError(req.method));
Expand Down
15 changes: 13 additions & 2 deletions test/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,13 @@ test('GH-379 static serves file with parentheses in path', function (t) {
});


test('GH-719 serve a specific static file', function (t) {
// serve the same default file .tmp/public/index.json
// but get it from opts.file
serveStaticTest(t, false, '.tmp', null, true);
});


test('audit logger timer test', function (t) {
// Dirty hack to capture the log record using a ring buffer.
var ringbuffer = new bunyan.RingBuffer({ limit: 1 });
Expand Down Expand Up @@ -1234,7 +1241,7 @@ test('tests the requestLoggers extra header properties', function (t) {


///--- Privates
function serveStaticTest(t, testDefault, tmpDir, regex) {
function serveStaticTest(t, testDefault, tmpDir, regex, staticFile) {
var staticContent = '{"content": "abcdefg"}';
var staticObj = JSON.parse(staticContent);
var testDir = 'public';
Expand All @@ -1254,8 +1261,13 @@ function serveStaticTest(t, testDefault, tmpDir, regex) {
fs.writeFile(file, staticContent, function (err3) {
t.ifError(err3);
FILES_TO_DELETE.push(file);
var p = '/' + testDir + '/' + testFileName;
var opts = { directory: tmpPath };

if (staticFile) {
opts.file = p;
}

if (testDefault) {
opts.defaultFile = testFileName;
routeName += ' with default';
Expand All @@ -1268,7 +1280,6 @@ function serveStaticTest(t, testDefault, tmpDir, regex) {
name: routeName
}, restify.serveStatic(opts));

var p = '/' + testDir + '/' + testFileName;
CLIENT.get(p, function (err4, req, res, obj) {
t.ifError(err4);
t.equal(res.headers['cache-control'],
Expand Down