Expressjs file download - not downloading

I can't seem to get my "download file" feature working using Expressjs.

//DOWNLOAD FILE
router.get('/snippets/download', function (req, res) {
  res.attachment("untitled.js");
  res.send("here is some javascript");
});

If I access this route in my browser the file downloads to my computer but not if I use an Angularjs request to the route.

Am I missing something?

You can use res.download. Refer documentation here: http://expressjs.com/4x/api.html

Eg:

//DOWNLOAD FILE
router.post('/snippets/download', function (req, res) {
    res.download(req.body.filename, req.body.text);
});

See if this helps.