在Nodejs / Express js中,我无法将响应返回到Angular 6

时间:2019-02-01 15:31:43

标签: express angular6

我正在使用Angular 6和Nodejs / Expressjs来避免跨域问题。这是我的代码

我在Angular中打电话:

this.httpClient.post('/uploadFile',  formData, {params: params})
.pipe(map((res: any) => {return res}),
catchError((error: HttpErrorResponse) => {})

Nodejs / Expressjs:

app.post('/uploadFile', (req, res) => {   
let formData
const form = new IncomingForm();
let readStream;

form.on('file', (field, file) => {
console.log('file.path>>>',file.path);
readStream = fs.createReadStream(file.path);   
});
form.on ('fileBegin', function(name, file) {
//rename the incoming file to the file's name
let fileName = file.path.split("\\");
fileName[fileName.length-1] = file.name.split('.')[0];
fileName = fileName.join("\\");
file.path = fileName;
console.log('file.path', file.path);
console.log('file.name', file.name);
})
form.parse(req, function (err, fields, files) {
formData = new FormData();
formData.append("file", readStream);
formData.append('package_name', req.query.packagename);
formData.append('type', req.query.type);
formData.append('version', req.query.version);
formData.append('descr', req.query.descr);

console.log('req.query.packagename',req.query.packagename);
const axiosConfig = {
    headers: {
        'Content-Type': 'multipart/form-data'
    }
};
let uploadRequest = request.post("WebAPiCallURL", requestCallback);
uploadRequest._form = formData;     
uploadRequest.setHeader('Content-Type', 'multipart/form-data');
function requestCallback(err, res, body) {
    return JSON.parse(body);            
}
}); 
});

我无法从requestCallback发送响应到Angular6

1 个答案:

答案 0 :(得分:0)

您没有将响应发送给客户端。要发送,可以使用import matplotlib.pyplot as plt # log-log f, ax = plt.subplots() ax.set(xscale='log', yscale='log') sns.regplot(x=X, y=y, ci=None, ax=ax) # log-linear f, ax = plt.subplots() ax.set(xscale='linear', yscale='log') sns.regplot(x=X, y=y, ci=None, ax=ax) 或Expressjs中的任何response function

res.send

注意: 当您使用相同的变量 function requestCallback(err, response, body) { //Rename res to response to avoid conflict res.send(body); // Send Response to Client } 进行请求时,请重命名为其他名称