无法代理 node.js 中的 PDF 文件
unable to proxy a PDF file in node.js
我需要从我从外部服务接收的端点下载 pdf,将 pdf 存储在我的数据库中并将其发送回浏览器(或任何在我的系统上调用 API 的人)。
出于某些原因,我无法理解如果我将服务的 URL 直接放在浏览器上它会正确打开 pdf,但是如果我 运行 此代码
var express = require('express');
var request = require('request');
var app = express();
var _PORT = 9008;
console.log("starting server on port " + _PORT);
// will return an unordered list of all items.
app.get('/', function(req, res) {
console.log("Proxying stuff");
var get_options = {
'rejectUnauthorized': false,
'url': 'http://www.urartuuniversity.com/content_images/pdf-sample.pdf',
'method': 'GET'
};
request(get_options, function(err, get_resp, body){
if(err){
console.log(err.message);
res.json({error: err})
}
console.log('data received from service:' + body.length);
res.type('application/pdf');
res.end(body);
});
});
app.listen(_PORT);
通过从浏览器调用 localhost:9008/
我只得到 2 个空白页。我尝试了所有更改所有可能的配置和编码,但到目前为止我只有空白页。
如果您需要更多信息,请询问。
请改变
var get_options = {
'rejectUnauthorized': false,
'url': 'http://www.urartuuniversity.com/content_images/pdf-sample.pdf',
'method': 'GET'
};
request(get_options, function(err, get_resp, body){
if(err){
console.log(err.message);
res.json({error: err})
}
console.log('data received from service:' + body.length);
res.type('application/pdf');
res.end(body);
});
到
request
.get('http://www.urartuuniversity.com/content_images/pdf-sample.pdf')
.on('response', function(response) {
console.log(response.statusCode)
console.log(response.headers['content-type'])
})
.on('end', function(){
// store pdf into DB here.
fs.createReadStream('pdf-sample.pdf').pipe(res); // send the pdf to client
})
.pipe(fs.createWriteStream('pdf-sample.pdf')) //store the pdf in your server
所以,我终于找到了怎么做:
request
.get('http://www.urartuuniversity.com/content_images/pdf-sample.pdf')
.on('response', function(response) {
var chunks = [];
response.on('data', function(chunk){
chunks.push(chunk);
});
response.on('end', function(){
var finalData = new Buffer.concat(chunks);
myDatabase.store(finalData);
res.type('application/pdf');
res.end(finalData, 'binary');
});
这可能超级简单,因为
app.use('/pdf', (req, res) => {
let url = 'http://www.urartuuniversity.com/content_images/pdf-sample.pdf';
let src = request(url);
req.pipe(src).pipe(res);
});
您不必以这种方式在服务器上存储数据。
我需要从我从外部服务接收的端点下载 pdf,将 pdf 存储在我的数据库中并将其发送回浏览器(或任何在我的系统上调用 API 的人)。 出于某些原因,我无法理解如果我将服务的 URL 直接放在浏览器上它会正确打开 pdf,但是如果我 运行 此代码
var express = require('express');
var request = require('request');
var app = express();
var _PORT = 9008;
console.log("starting server on port " + _PORT);
// will return an unordered list of all items.
app.get('/', function(req, res) {
console.log("Proxying stuff");
var get_options = {
'rejectUnauthorized': false,
'url': 'http://www.urartuuniversity.com/content_images/pdf-sample.pdf',
'method': 'GET'
};
request(get_options, function(err, get_resp, body){
if(err){
console.log(err.message);
res.json({error: err})
}
console.log('data received from service:' + body.length);
res.type('application/pdf');
res.end(body);
});
});
app.listen(_PORT);
通过从浏览器调用 localhost:9008/
我只得到 2 个空白页。我尝试了所有更改所有可能的配置和编码,但到目前为止我只有空白页。
如果您需要更多信息,请询问。
请改变
var get_options = {
'rejectUnauthorized': false,
'url': 'http://www.urartuuniversity.com/content_images/pdf-sample.pdf',
'method': 'GET'
};
request(get_options, function(err, get_resp, body){
if(err){
console.log(err.message);
res.json({error: err})
}
console.log('data received from service:' + body.length);
res.type('application/pdf');
res.end(body);
});
到
request
.get('http://www.urartuuniversity.com/content_images/pdf-sample.pdf')
.on('response', function(response) {
console.log(response.statusCode)
console.log(response.headers['content-type'])
})
.on('end', function(){
// store pdf into DB here.
fs.createReadStream('pdf-sample.pdf').pipe(res); // send the pdf to client
})
.pipe(fs.createWriteStream('pdf-sample.pdf')) //store the pdf in your server
所以,我终于找到了怎么做:
request
.get('http://www.urartuuniversity.com/content_images/pdf-sample.pdf')
.on('response', function(response) {
var chunks = [];
response.on('data', function(chunk){
chunks.push(chunk);
});
response.on('end', function(){
var finalData = new Buffer.concat(chunks);
myDatabase.store(finalData);
res.type('application/pdf');
res.end(finalData, 'binary');
});
这可能超级简单,因为
app.use('/pdf', (req, res) => {
let url = 'http://www.urartuuniversity.com/content_images/pdf-sample.pdf';
let src = request(url);
req.pipe(src).pipe(res);
});
您不必以这种方式在服务器上存储数据。