Wayne Hartman
Wayne Hartman

Reputation: 18477

Restify: How to serve static files while not conflicting with other routes?

I have a node.js app I am creating and I am using restify. I have setup a GET route for some data, but I am also wanting to serve static content from my root.

GET route:

server.get({path : SURVEY_QUESTION_PATH, version : appVersion}, getLandingSurveyQuestions);

Static routes:

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

This static route is conflicting with my dynamic route: How do I serve up this static content, without conflicting with the other routes I have? I'd hate to have to setup static routes for all of the individual files in my project.

I have tried:

  1. Changing the order of the route registration
  2. Tweaking the regex of the static route

Upvotes: 3

Views: 615

Answers (1)

Wayne Hartman
Wayne Hartman

Reputation: 18477

So far I have a hack: changing the regex to include the common static files that will be served up. This seems inefficient, but works.

server.get(/(^\/$)|(\.(html|js|css|png|jpg)$)/, restify.serveStatic({
  directory: 'static',
  default: 'index.html'
}));

Upvotes: 4

Related Questions