【发布时间】:2016-01-10 21:12:32
【问题描述】:
我有一个使用 ExpressJS 构建在 MEAN 堆栈上的应用程序。
例如,需要从后端 (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();
});
}
});
});
有什么方法可以将结果从 NodeJS 发送到 AngularJS 控制器?
有一个类似的问题here,但没有解决。
【问题讨论】: