node-restify icon indicating copy to clipboard operation
node-restify copied to clipboard

NotAuthorized when serving static files.

Open trevorstarick opened this issue 11 years ago • 14 comments

I'm trying to serve a static index.html file and I think my code is correct but when I go to http://localhost/static I get:

{
  code: "NotAuthorized",
  message: "/static"
}

Is it an issue with the current changes that have been made to the static file serving JS or is it an issue with my code?

Route:

app.get(/\/static\/?.*/, restify.serveStatic({ 
    directory: "./",
    default: "index.html"
}));

trevorstarick avatar Mar 05 '14 22:03 trevorstarick

restify does not do authorization checks for you, so you must have put those in somewhere?

On Wed, Mar 5, 2014 at 2:36 PM, Trevor Starick [email protected]:

I'm trying to serve a static index.html file and I think my code is correct but when I go to http://localhost/static I get:

{ code: "NotAuthorized", message: "/static" }

Is it an issue with the current changes that have been made to the static file serving JS or is it an issue with my code?

Route:

app.get(//static/?.*/, restify.serveStatic({ directory: "./", default: "index.html" }));

Reply to this email directly or view it on GitHubhttps://github.com/mcavage/node-restify/issues/549 .

mcavage avatar Mar 05 '14 22:03 mcavage

What do you mean by authorization checks. As in OAuth/Basic?

trevorstarick avatar Mar 05 '14 23:03 trevorstarick

Solved by changing "./" to __dirname

app.get(/\/static\/?.*/, restify.serveStatic({
        directory: __dirname,
        'default': 'index.html'
    }));

trevorstarick avatar Mar 05 '14 23:03 trevorstarick

As in restify will not return a 403 unless you have something in the chain to do that.

On Wed, Mar 5, 2014 at 3:02 PM, Trevor Starick [email protected]:

What do you mean by authorization checks. As in OAuth/Basic?

Reply to this email directly or view it on GitHubhttps://github.com/mcavage/node-restify/issues/549#issuecomment-36805777 .

mcavage avatar Mar 05 '14 23:03 mcavage

Strange since this is my code I'm using.

var restify = require('restify');
var register = function(app) {
    app.get('/', function(req, res, next) {
        res.send('hw');
        next();
    });
    app.get(/\/static\/?.*/, restify.serveStatic({
        directory: './',
        default: 'index.html'
    }));
};
var http = restify.createServer();
register(http);
http.listen(80);

I don't specify 403's anywhere

trevorstarick avatar Mar 05 '14 23:03 trevorstarick

Which version are you using, on what operating system? Your code works fine for me. Also, you should use a port number greater than 1024, so you won't need super user rights to run it.

gergelyke avatar Mar 06 '14 07:03 gergelyke

the serveStatic function definitely does pass a NotAuthorizedError: https://github.com/mcavage/node-restify/blob/master/lib/plugins/static.js#L105

kaii-zen avatar Mar 06 '14 15:03 kaii-zen

I'm seeing the same thing, I'm not sure where it's coming from (I agree it seems impossible). It started when I used npm to re-install Restify in an effort to troubleshoot a bodyParser/multipart issue.

jjg avatar Apr 28 '14 19:04 jjg

path.join returns the filename normalized without a leading . (e.g., ./public/index.html becomes public/index.html). But the check @sbergmann refers to tests whether the filename begins with a . and returns the error if not. Perhaps normalization is O/S-dependent, but on a Mac it looks like it's impossible in 2.8.2. to not hit a NotAuthorizedError. I had to change the line to file = '.' + path.join... to get it to work.

zepheiryan avatar Sep 13 '14 01:09 zepheiryan

I can confirm that it is impossible to not hit a NotAuthorizedError when trying to serve static files. Explanation same as above.

phanimahesh avatar May 10 '15 15:05 phanimahesh

I got this to work (inspired by @zepheiryan ) by changing this line to var file = './' + path.join(opts.directory,.

node-restify v0.12.7 (via npm) Debian 8 Node v0.12.7

playeren avatar Aug 10 '15 14:08 playeren

I got the same issue, too. I fixed it by changing directory: ''. to directory: __dirname,. node-restify ^4.0.2 Node 4.0.0

CocaCola183 avatar Sep 14 '15 09:09 CocaCola183

I was having this exact issue with no auth checks in my code. Switching from directory: '' to directory: __dirname fixed the problem for me too.

On Windows 10 Node 5.8.0 Restify 4.0.4

yerol avatar Mar 22 '16 12:03 yerol

The correct error for this would be ForbiddenError!

kolbma avatar May 23 '22 15:05 kolbma