简体   繁体   English

如何使用 node.js 从服务器向 Angular 前端提供 30GB 的 pdf 文件(静态)?

[英]How to serve 30GB of pdf files(static) from server using node.js to Angular front end?

I have stored the path of folder which contains files in Mongodb database field but Angular does not take folder outside src in build so can't access files in folder should I send files from node using res.sendfile()?我已经在 Mongodb 数据库字段中存储了包含文件的文件夹的路径,但是 Angular 在构建中不会在 src 之外获取文件夹,所以如果我应该使用 res.sendfile() 从节点发送文件,则无法访问文件夹中的文件?

Is there any way to access files in Angular without copying the folder in assets folder?because of the size I can't put files in assets folder.有没有办法在不复制资产文件夹中的文件夹的情况下访问 Angular 中的文件?因为我无法将文件放在资产文件夹中。

Help is required as I'm totally confused and stuck at this problem for over 2 weeks.需要帮助,因为我完全困惑并在这个问题上停留了超过 2 周。

A simple way to create a web server and serve your files from that folder, assuming you are in a node.js environment you can create web server with express js一种创建 Web 服务器并从该文件夹提供文件的简单方法,假设您在 node.js 环境中,您可以使用 express js 创建 Web 服务器

const express = require('express')
const app = express();

//create a folder named files in the directory put files in this folder.
app.use(express.static('files'))

app.get('/', (req, res) => res.send('Assets server'));


app.listen(3000, () => console.log('listening on port 3000!'))

And you can access the files with urls like this way (You don't need to put in the angular assets folder).您可以像这样使用 url 访问文件(您不需要放入 angular assets 文件夹)。

eg url例如网址

eg:localhost:3000/files/file1.pdf

我不是 100% 确定我理解您的问题,但也许您可以使用符号链接将包含所有文件的文件夹链接到您的网站可以访问的另一个文件夹(在 Angular 网站结构下)?

I think this will be helpful我认为这会有所帮助

const express = require('express');
const app = express();
const fs = require('fs')

app.get('/asset/:pdfname', function (request, response) {
const pdfname= request.params.pdfname;
const tempFile = "./public/pdf/"+ pfdname +".pdf";
fs.readFile(tempFile, function (err, data) {
    response.contentType("application/pdf");
    response.send(data);
  });
});

app.listen(3000, () => {
  console.log('Server is running on Port 3000')
})

call url in browser在浏览器中调用 url

localhost:3000/asset/pdfname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM