1

I'm creating a simple testing platform for an app and have the following code setup as my server.js file in the root of my app:

var restify = require('restify'),
    nstatic = require('node-static'),
    fs = require('fs'),
    data = __dirname + '/data.json',
    server = restify.createServer();


// Serve static files
var file = new nstatic.Server('');
server.get(/^\/.*/, function(req, res, next) {
    file.serve(req, res, next);
});


// Process GET
server.get('/api/:id', function(req, res) {
    // NEVER FIRES
});

It serves static files perfectly, however, when I try to make a call to the /api it just hangs and times out. Imagine I'm missing something stupid here, any help would be greatly appreciated.

Fluidbyte
  • 5,162
  • 9
  • 47
  • 76
  • You could check the header to see whats going on when you attempt to hit the api and/or put a breakpoint on your api method to see if it ever gets hit. – Christopher Marshall Apr 10 '13 at 20:08
  • I `console.log`ed the `/api` get call and it just never reaches it. Beyond that it just times out. – Fluidbyte Apr 10 '13 at 20:10
  • 2
    Does nstatic `serve` use `next` when a file cannot be served? Otherwise you would have to move the second get before the first. – atondelier Apr 10 '13 at 20:12

2 Answers2

3

node-static is calling next with an error, which means it's never yielding to other handlers.

You can move your other handlers above node-static or ignore it's errors by intercepting it's callback.

I made a working version here: http://runnable.com/UWXHRONG7r1zAADe

generalhenry
  • 17,227
  • 4
  • 48
  • 63
2

You may make yourself sure the api get call is caught by moving the second get before the first. The reason is your api calls routes are already matched by the first pattern.

atondelier
  • 2,424
  • 15
  • 11