2

I have an application built on the MEAN stack, with ExpressJS.

There is a need to send success or error messages from backend (NodeJS) to frontend (AngularJS) for example - to show when a file was successfully uploaded. Below - after app.post is successfully completed, I would like to show this result on frontend (in AngularJS).

app.post('/upload' , function (req, res) {
        //busboy handles multi-part file upload
        console.log('Post received to upload ...');

        var fstream;

        req.pipe(req.busboy);

        req.busboy.on('file', function (fieldname, file, filename) {
            //only update if the filename was change or exists
            if (filename!== undefined || filename !=='') {
                  //reset url
                  options.url = '';
                 console.log("Uploading the file " + filename);
                  console.log("before", options);
                  options.path = '/uploads/'  + filename;
                  options.fileName = filename; //update file name

                  console.log("Updating options ...", options);
                  fstream = fs.createWriteStream(__dirname + '/uploads/' + filename); //path where the file will be uploaded
                  file.pipe(fstream);
                  fstream.on('close', function () {
                      console.log("Upload finished successfully ! Uploaded " + filename);
                      initOntology();//initialize the ontology from upload
                      initEdges();
                  });
            }

        });
});

Is there any way that I can send the result from NodeJS to AngularJS controller?

There is a similar question here, but unsolved.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
julia
  • 452
  • 6
  • 15

2 Answers2

0

You should use socket.io

When the frontend starts up, it connects to a 'socket connection'. This is a different protocol to HTTP. If it can't do so, it uses a degraded version of the connection which works over HTTP.

Either end of the connection can 'emit' a message, which gets sent to the other end. The backend can send messages to all clients or to specific ones. You would set up Angular to receive the message and create its own event which one or more controller could be listening for. See https://github.com/btford/angular-socket-io When the frontend wants to send a message back to the backend, it can use the socket, or just a regular HTTP POST, etc.

Using this with Angular and Node is quite standard, there should be lots of information out there on how to do it.

jwg
  • 5,547
  • 3
  • 43
  • 57
0

Just send a http response back using the res object :

res.status(200).send('OK') 

When error, send, a error status

res.status(500).send(errorMsg)

For angular :

$http.post(...).then(function successCallback(response) {
      // response with 200 status
      }, function errorCallback(response) {
      // response with status 500
  });
Moncef Hassein-bey
  • 1,361
  • 9
  • 14
  • "Cannot set header after they are sent" this is the node.js error when I try to use the res.status responses. – julia Oct 16 '15 at 17:05