Skip to content

Commit

Permalink
Change default relative path prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Feb 28, 2013
1 parent a0dedd7 commit 8d6dac5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
7 changes: 4 additions & 3 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ function onRequest(request, next) {

**hapi** provides built-in support for serving static files and directories as described in [File](#file) and [Directory](#directory).
When these handlers are provided with relative paths, the `files.relativeTo` server option determines how these paths are resolved
and defaults to _'process'_:
and defaults to _'cwd'_:
- _'cwd'_ - relative paths are resolved using the active process path (_'process.cwd()'_).
- _'routes'_ - relative paths are resolved based on the location of the files in which the server's _'route()'_ method is called. This means the location of the source code determines the location of the static resources when using relative paths.
- _'process'_ - relative paths are resolved using the active process path (_'process.cwd()'_).
- an absolute prefix path - an absolute path (e.g. '/path') used as a prefix for all relative paths.


### Views
Expand Down Expand Up @@ -834,7 +835,7 @@ configured with a static file path. For example:
```javascript
// Create Hapi server
var http = new Hapi.Server('0.0.0.0', 8080, { files: { relativeTo: 'process' } });
var http = new Hapi.Server('0.0.0.0', 8080, { files: { relativeTo: 'cwd' } });

// Serve index.html file up a directory in the public folder
http.route({ method: 'GET', path: '/', handler: { file: './public/index.html' } });
Expand Down
2 changes: 1 addition & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ exports.server = {
// Files path

files: {
relativeTo: 'process' // Determines what file and directory handlers use to base relative paths off: 'routes', 'process'
relativeTo: 'cwd' // Determines what file and directory handlers use to base relative paths off: 'cwd', 'routes', or absolute path prefix
},

// timeout limits
Expand Down
15 changes: 11 additions & 4 deletions lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,30 @@ exports.directoryHandler = function (route, options) {

internals.absolutePath = function (route) {

Utils.assert(route.server.settings.files && route.server.settings.files.relativeTo && ['routes', 'process'].indexOf(route.server.settings.files.relativeTo) !== -1, 'Invalid server files.relativeTo configuration');
var relativeTo = route.server.settings.files && route.server.settings.files.relativeTo;
Utils.assert(relativeTo && (relativeTo[0] === '/' || ['cwd', 'routes'].indexOf(relativeTo) !== -1), 'Invalid server files.relativeTo configuration');

// Plugin

if (route.env.path) {
return route.env.path;
}

// 'cwd'

if (relativeTo === 'cwd') {
return '.';
}

// 'routes'

if (route.server.settings.files.relativeTo === 'routes') {
if (relativeTo === 'routes') {
return internals.getRouteSourceFilePath();
}

// 'process'
// '/path'

return process.cwd();
return relativeTo;
};


Expand Down
20 changes: 6 additions & 14 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@ describe('Response', function () {
});
});

it('returns a file in the response with the correct headers using process relative paths', function (done) {
it('returns a file in the response with the correct headers using cwd relative paths', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'process' } });
var server = new Hapi.Server(0, { files: { relativeTo: 'cwd' } });
server.route({ method: 'GET', path: '/', handler: { file: './package.json' } });

server.start(function () {
Expand All @@ -444,13 +444,9 @@ describe('Response', function () {

it('returns a 404 when the file is not found', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'routes' } });
var notFoundHandler = function (request) {

request.reply(new Hapi.Response.File(__dirname + '/../../notHere'));
};
var server = new Hapi.Server(0, { files: { relativeTo: '/no/such/path/x1' } });

server.route({ method: 'GET', path: '/filenotfound', handler: notFoundHandler });
server.route({ method: 'GET', path: '/filenotfound', handler: { file: 'nopes' } });

server.start(function () {

Expand All @@ -465,13 +461,9 @@ describe('Response', function () {

it('returns a 403 when the file is a directory', function (done) {

var server = new Hapi.Server(0, { files: { relativeTo: 'routes' } });
var folderHandler = function (request) {

request.reply(new Hapi.Response.File(__dirname));
};
var server = new Hapi.Server(0);

server.route({ method: 'GET', path: '/filefolder', handler: folderHandler });
server.route({ method: 'GET', path: '/filefolder', handler: { file: 'examples' } });

server.start(function () {

Expand Down

0 comments on commit 8d6dac5

Please sign in to comment.