linemanjs / lineman

Lineman helps you build fat-client JavaScript apps. It produces happiness by building assets, mocking servers, running specs on every file change
MIT License
1.18k stars 83 forks source link

Watching API stub files folder #380

Open BurningDog opened 8 years ago

BurningDog commented 8 years ago

At https://github.com/linemanjs/lineman/issues/318 it's suggested to use the config/stubs folder for API stub files. I think this is a good suggestion and am using this for a local project.

@davemo noted at https://github.com/linemanjs/lineman/issues/318#issuecomment-62154875 that "The most annoying part of working in this way is having to restart lineman when a stubbed file changes."

This pull request solves that issue - an API file can be edited and is immediately available via Express.

davemo commented 8 years ago

This seems ok, I guess I wonder what happens if there are no files in config/stubs and if this breaks when you end up with n files and delete them to get to no files. We've seen some weirdness with the file watching happen in those scenarios before, so if you can verify nothing breaks in that case with this code change I'd be ok with this change.

BurningDog commented 8 years ago

No files in config/stubs

Lineman starts up as normal; no warnings printed.

Having n files then deleting to get no files

After deleting my API files then doing a GET on a previously working stub, I get a "Not found" returned by Express but nothing breaks. In server.js I have:

app.get('/api/1.0/goals', function (req, res) {
      fs.readFile('config/stubs/1.0/goals.json', function(err, data) {
        if (err) {
          res.status(404).send('Not found');
        } else {
          res.header('Cache-Control', 'none').contentType('application/json').send(data);
        }
        res.end();
      });
    });