Javascript 带有接收图像的Express JS渲染视图

Javascript 带有接收图像的Express JS渲染视图,javascript,html,api,express,httprequest,Javascript,Html,Api,Express,Httprequest,我正在使用两个Express JS应用程序,一个是API,第二个是通过发出请求并向用户显示收到的信息来使用此API的应用程序 在API路径中,我将图像作为响应发送: router.get('/:customer_id',authController.isAuthenticated,(req,res) => { . . Retrieving customer data . return res.sendFile('/uploads/'+foundCustom

我正在使用两个
Express JS
应用程序,一个是
API
,第二个是通过发出请求并向用户显示收到的信息来使用此
API
的应用程序

API
路径中,我将图像作为响应发送:

router.get('/:customer_id',authController.isAuthenticated,(req,res) => {
    .
    . Retrieving customer data
    .

    return res.sendFile('/uploads/'+foundCustomer.doc_path);
});
稍后,另一个应用程序将获取此
文档

router.get('/:customer_id',(req,res) => {
    var options = {
    url: 'http://'+config.API.user+':'+config.API.password+'@'+config.API.host+':'+config.API.port+'/customers/'+req.params.customer_id
    };

    request(options,(err,response,body)=>{
        return res.render('customer/show',{
            document: ?, // Send document as parameter to view
        });
    });
});
在这一点上,我想用customer
document
呈现
customer/show
(EJS视图引擎),但我不想将此文档保存在我的应用程序文件中,因为
document
只需要在视图中显示(客户详细信息和文档存储在另一个应用程序中)

我试图在我的应用程序结构中创建
临时
目录,但很难管理删除那些不需要的文档(应用程序有许多
用户
,同时可以显示许多
客户

我试图实现的另一个解决方案是在客户端发出
Ajax
请求,后者将收到的文档附加到
。但是这个请求必须通过
用户
密码
进行身份验证,所以我意识到在客户端javascript上存储凭据不是最好的主意

我不确定是否可以在不保存应用程序文件的情况下渲染和显示图像


如果有任何帮助,我将不胜感激,也许最好的解决办法是以某种方式临时管理
保存的文档。

为什么不在EJS模板中创建一个文件对象,然后将其用于
上的
src
属性?您已经从映像API服务器获取原始缓冲区/blob。将其存储在模板中。

来自

最终解决方案(已测试),用于制作
API
请求:

在我的路径中,我将向我的
API
发出HTTP请求,以检索
PDF
文件(文档):

在我的
ejs
视图中,我使用HTML
object
标记来显示收到的PDF:

<object data="data:application/pdf;base64,<%-document%>"></object>


我有一些语法错误。我试图在EJS文件中构建
aBlob
对象,如下所示:
var-aBlob=new-Blob(“”)其中
customer\u文档
是从API接收的字符串。我收到“Uncaught SyntaxError:Invalid or unexpected token”错误,请删除周围的引号。将字符串转换为buffer new buffer()
var buffer=new buffer()此语法或带引号的语法也给出了错误。您是否有以前项目中测试过的代码示例?您的
。。它是图像文件吗?
var objectURL = URL.createObjectURL( aBlob ); // Where object is a Blob object
const img = document.createElement('img');
img.src = objectURL;
axios.get(`http://my-api/customer/id`).then(response => {
   var photo =  new Buffer(response.data, 'binary').toString('base64');

   return res.render('customers/show',{
       document: document
   });
});
<object data="data:application/pdf;base64,<%-document%>"></object>