Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodejs中未调用Angular 5 http post方法_Javascript_Node.js_Ajax_Angular_Angular5 - Fatal编程技术网

Javascript nodejs中未调用Angular 5 http post方法

Javascript nodejs中未调用Angular 5 http post方法,javascript,node.js,ajax,angular,angular5,Javascript,Node.js,Ajax,Angular,Angular5,我有一个nodejs作为后端服务,angular 5作为前端,我想调用一个POST方法到nodejs服务器,向服务器发送一个Blob文件 但该post方法被执行,并且在后端控制台日志上没有显示任何内容 以下是一些代码片段: server.js const express = require('express'); const app = express(); const port = process.env.PORT || 3000; const bodyParser = require('bo

我有一个nodejs作为后端服务,angular 5作为前端,我想调用一个POST方法到nodejs服务器,向服务器发送一个Blob文件

但该post方法被执行,并且在后端控制台日志上没有显示任何内容

以下是一些代码片段:

server.js

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const bodyParser = require('body-parser');
const cors = require('cors');
app.use(cors());
//create a cors middleware
app.use(function (req, res, next) {
  //set headers to allow cross origin request.
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
app.post('/decrypt', function (req, res) {
  console.log("decrypt called");
  return 'data back';
})
在AngularJS中: database.services.ts:

import { Injectable, Input } from '@angular/core';
import { Http, Response, ResponseContentType } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import { Observable } from 'rxjs/Observable';
import { saveAs, FileSaver } from 'file-saver/FileSaver';
import { HttpClientModule, HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';

@Injectable()
export class DatabaseService {

  private API_GET_LIST_FILES = 'http://localhost:3000/files';

  private BASE_URL = 'http://localhost:3000/';

  private API_GET_FILE = 'http://localhost:3000/download?name=';

  constructor(private http: Http) { }

  getFile(key: string) {
    return this.http.get(this.API_GET_FILE + key, {
      responseType: ResponseContentType.Blob
    })
      .map(res => {
        return {
          filename: key.split('/').pop(),
          data: res.blob()
        };
      })
      .subscribe(res => {
        this.decryptFile(res.data);
        saveAs(res.data, res.filename);
      }, error => {
        console.log('download error:', JSON.stringify(error));
      }, () => {
        console.log('Completed file download.');
      })
  }

  decryptFile(data: any): Observable<any> {
    const httpOptions = {
      Headers: new HttpHeaders({
        'Content-Type': 'application/octet-stream',
        'data': data
      })
    };
    console.log(data, typeof data, this.BASE_URL + `decrypt`);
    return this.http.post(`http://localhost:3000/decrypt`, httpOptions);
  }
}
从'@angular/core'导入{Injectable,Input};
从'@angular/Http'导入{Http,Response,ResponseContentType};
导入'rxjs/add/operator/map';
导入'rxjs/add/operator/do';
从“rxjs/Observable”导入{Observable};
从'file saver/FileSaver'导入{saveAs,FileSaver};
从“@angular/common/http”导入{HttpClientModule,HttpClient,HttpHeaders,HttpParams};
@可注射()
导出类数据库服务{
私有API\u获取\u列表\u文件=http://localhost:3000/files';
私人基地http://localhost:3000/';
私有API_GET_FILE='1〕http://localhost:3000/download?name=';
构造函数(私有http:http){}
getFile(键:字符串){
返回此.http.get(this.API\u get\u文件+密钥{
responseType:ResponseContentType.Blob
})
.map(res=>{
返回{
文件名:key.split('/').pop(),
数据:res.blob()
};
})
.订阅(res=>{
此.decrypt文件(res.data);
saveAs(res.data,res.filename);
},错误=>{
log('download error:',JSON.stringify(error));
}, () => {
log('已完成文件下载');
})
}
解密文件(数据:任意):可观察{
常量httpOptions={
标题:新的HttpHeaders({
“内容类型”:“应用程序/八位字节流”,
“数据”:数据
})
};
log(数据,数据类型,this.BASE_URL+`decrypt`);
返回this.http.post(`http://localhost:3000/decrypt`,httpOptions);
}
}
一旦我单击页面上的文件下载按钮,就会调用这个getFile函数,因为在浏览器控制台中,它将打印出
Blob(564534){size:564534,键入:“application/octet stream”}“object”http://localhost:3000/decrypt“

我希望nodejs服务器将此post方法和Blob(GPG文件)对象作为参数,并执行一些操作

但看起来后端服务器没有打印任何内容

请告知如何修改此代码,我应该使用POST还是PUT?我想将一个GPG文件传递到nodeJS服务器并解密它


谢谢,

解密文件
返回
可观察的
,您必须订阅它才能执行http调用:

this.decrypt文件(res.data).subscribe(decrypted=>{???})


编辑:无法抗拒,一些观察,请随意忽略:

  • 为什么要从服务器获取文件,然后将其发送回服务器?为什么不在第一次API调用时解密文件并返回它呢
  • 从安全的角度来看。。。没关系,我只是希望在您处理PGP文件时,服务器上会有一些授权
  • 最好从
    @angular/common/http
    开始使用
    HttpClient
    ,而不是
    http
    。好消息是你已经进口了
  • 根据文件的大小,您可能需要考虑使用HTTP上载而不是POST。不过,请看第一点
日志中有哪些错误?您的请求的网络状态是400还是404?也可能需要考虑使用代理。嗨,你能帮我看看我的另一个问题吗?