I have an application built on the MEAN stack, with Expressjs.
我有一个基于MEAN堆栈的应用程序,使用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 succesfully completed, I would like to show this result on frontend (in AngularJS).
需要从后端(nodejs)向前端(angularjs)发送成功或错误消息,以显示文件成功上载的时间。下面 - 在app.post成功完成后,我想在前端(在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?
有什么方法可以将结果从NodeJS发送到AngularJS控制器吗?
There is a similar question here, but unsolved.
这里有一个类似的问题,但尚未解决。
2 个解决方案
#1
1
You should use socket.io
你应该使用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.
当前端启动时,它连接到“套接字连接”。这是与HTTP不同的协议。如果它不能这样做,它使用通过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.
连接的任何一端都可以“发出”一条消息,然后发送到另一端。后端可以向所有客户端或特定客户端发送消息。您可以设置Angular来接收消息并创建自己的事件,一个或多个控制器可以监听它。请参阅https://github.com/btford/angular-socket-io当前端想要将消息发送回后端时,它可以使用套接字,或者仅使用常规HTTP POST等。
Using this with Angular and Node is quite standard, there should be lots of information out there on how to do it.
使用Angular和Node是非常标准的,应该有很多关于如何做的信息。
#2
1
Just send a http response back using the res object :
只需使用res对象发回http响应:
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
});
#1
1
You should use socket.io
你应该使用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.
当前端启动时,它连接到“套接字连接”。这是与HTTP不同的协议。如果它不能这样做,它使用通过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.
连接的任何一端都可以“发出”一条消息,然后发送到另一端。后端可以向所有客户端或特定客户端发送消息。您可以设置Angular来接收消息并创建自己的事件,一个或多个控制器可以监听它。请参阅https://github.com/btford/angular-socket-io当前端想要将消息发送回后端时,它可以使用套接字,或者仅使用常规HTTP POST等。
Using this with Angular and Node is quite standard, there should be lots of information out there on how to do it.
使用Angular和Node是非常标准的,应该有很多关于如何做的信息。
#2
1
Just send a http response back using the res object :
只需使用res对象发回http响应:
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
});