-1

I'm trying to use a node backend with my angular script using Q to promise-fy an fs method. Using the normal callback method works, however here, nothing is returned to Angular. Nothing is printed in the console, and no error is returned in the console or in the terminal where node is running. What have I missed?

var express = require('express');
var app = express();
var router = express.Router();
var FS = require('fs');
var Q = require('q');

app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/dist', express.static(__dirname + '/dist'));

app.get('/', function(req, res) {
    res.sendfile('./index.html');
});

app.get('/test', function(req, res){
    // res.send('asdf')
    var deferred = Q.defer();
    FS.readFile("gulpfile.js", "utf-8", function (error, text) {
        console.log('error', error);
        console.log('in read file')
        if (error) {
            deferred.reject(new Error(error));
        } else {
            res.send(text);
            deferred.resolve(text);
        }
    });
    return deferred.promise;
});

app.listen(5000);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1252748
  • 14,597
  • 32
  • 109
  • 229
  • 1
    `deferred.resolve(text)` in your Node file ?! then `return deferred.promise;` ! – Ismail RBOUH Jul 19 '16 at 14:58
  • @IsmailRBOUH my mistake, that was a bad copy paste. That is currently what have. – 1252748 Jul 19 '16 at 15:08
  • But you still need to resolve your deferred `deferred.resolve(text)` ! – Ismail RBOUH Jul 19 '16 at 15:10
  • What does the network inspector show? I bet this is a problem on the server side and has nothing to do with angular. – Bergi Jul 19 '16 at 19:22
  • @Bergi There is no error error in Terminal or the console. The contents of the file are printed in Terminal, so I don't think there's any error server side. – 1252748 Jul 19 '16 at 22:15
  • @Bergi I updated the server with the complete code – 1252748 Jul 19 '16 at 22:17
  • I don't see any server side code that would print the contents of the file to the terminal... are you also missing that portion of the code? – GPicazo Jul 19 '16 at 22:26
  • @1252748 I mean the [network panel](https://developer.chrome.com/devtools/docs/network), neither the terminal nor the console. Does the expected result arrive at the client or not? – Bergi Jul 19 '16 at 22:27
  • @GPicazo I added the console statement where that happens. – 1252748 Jul 19 '16 at 22:27
  • @Bergi Ah or course. It just hangs on "Pending" – 1252748 Jul 19 '16 at 22:28
  • @Bergi It works if use the code if updated now. I just use `res.send(text)` before `deferred.resolve(text)`. So what does resolving the promise even do if it can't return it to the page that's requesting it? Is it just cleanup? Or for if your'e just running a node script from terminal. – 1252748 Jul 19 '16 at 22:35

1 Answers1

2

Express does not support promises out of the box. Returning a promise from a handler means nothing to it. You still have to call res.send explicitly (although there are modules such a express-promise to ease this).

This is what your code should look like:

app.get('/test', function(req, res){
    var promise = Q.ninvoke(FS, "readFile", "gulpfile.js", "utf-8");
    promise.then(function (text) {
        res.status(200).send(text);
    }, function(error) {
        console.error(error);
        res.status(500).send("oops");
    });
});
1252748
  • 14,597
  • 32
  • 109
  • 229
Bergi
  • 630,263
  • 148
  • 957
  • 1,375