Search code examples
node.jshttprestify

serving static files with restify (node.js)


I have the following code:

app.js

[...]

server.get(/\/docs\/public\/?.*/, restify.serveStatic({
  directory: './public'
}));

server.listen(1337, function() {
  console.log('%s listening at %s', server.name, server.url);
});

And I have the following file structure

app.js
public/
  index.html

So I try browsing:

http://localhost:1337/docs/public/index.html

and I get

{
  code: "ResourceNotFound",
  message: "/docs/public/index.html"
}

I tried with several variations, but none of them seemed to work.

I'm sure it should be something pretty obvious I'm missing


Solution

  • restify will use the directory option as a prefix for the entire route path. In your case, it will look for ./public/docs/public/index.html.