简体   繁体   English

尝试从前端加载.tsv文件。 该文件保留在我的服务器上。 如何使用Express做到这一点?

[英]Trying to load a .tsv file from the front end. The file stays on my server. How do I do this using Express?

I have an function like this on my Express server, which is sending a file that depends on D3.JS. 我的Express服务器上有一个类似的功能,该功能正在发送依赖于D3.JS的文件。

app.get('/dashboard', function(req, res) {
   var timestamp = utility.timestamp();
   console.log('[' + timestamp + '] Request made to render dashboard.')
  res.sendFile(__dirname + '/front_end/index.html')
});

This appears to be working correctly. 这似乎工作正常。

Once the file is served, it needs a data file in order to render properly. 提供文件后,它需要一个数据文件才能正确呈现。 I attempt to access it as follows. 我尝试按如下方式访问它。

d3.tsv("../data/data.tsv", function(error, data) { // DO STUFF HERE }

Anyway, needless to say, because the file that is being sought is actually on the backend, I'm not able to access it this way from the code I have served the user. 无论如何,不​​用说,因为正在查找的文件实际上在后端,所以我无法从提供给用户的代码中以这种方式访问​​它。

I have also tried hitting an endpoint that sends the .tsv file. 我也尝试过点击发送.tsv文件的端点。 However, there is an error that has to do with cross domain requests. 但是,存在与跨域请求有关的错误。

d3.tsv("localhost:3000/userAccessData", function(error, data) { // DO SAME STUFF }

Does anyone know an effective way to send either both files using Express or access the file on the server using a request? 有谁知道使用Express发送两个文件或使用请求访问服务器上文件的有效方法吗?

I initially fixed this problem by placing the resource file in a folder labeled as public. 我最初通过将资源文件放置在标记为public的文件夹中来解决此问题。 :D :D

Later, I found out that the browser actually caches the resource on the front-end and does not re-request it each time. 后来,我发现浏览器实际上在前端缓存了资源,并且没有每次都重新请求资源。 To circumvent that and ensure up to date data is available, I added sockets.io to my project as well. 为了避免这种情况并确保提供最新数据,我也向我的项目中添加了sockets.io。

暂无
暂无

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

相关问题 如何同时运行我的前端反应和后端反应? - How do I run my react front-end and express back-end together? 如何使用快速服务器将 zip 文件从后端发送到前端 - how to send a zip file back end to front end in nodejs using express server 如何从高于我的服务器文件夹级别的目录在 Express 中提供 HTML 文件? - How do I serve an HTML file in Express from a directory which is a level above my server folder? 如何在AngularJS中使用D3加载节点服务器的tsv文件 - How can I load tsv file of node server using D3 in AngularJS 如何配置我的 Express 服务器以呈现我的所有内容,而不仅仅是我的 HTML 文件? - How do I configure my Express server to render all of my content, not just my HTML file? 如何在使用 Ract 作为前端时从 express 渲染 HTML 文件? - How to render HTML file from express while using Ract as front end? 如何从 node/express 中的后端(端口:3001)路由重定向到前端(端口:3000)? - How do I redirect to front-end (port: 3000) from a back-end (port:3001) route in node/express? 如何在给定的快递文件中分隔路线? - How do I separate routes in my given express file? 为什么我不需要在我的 html 文件中引用相对路径来加载带有 express js 的 javascript 文件? - Why do I not need to reference the relative path in my html file to load a javascript file with express js? 如何使用我的 NODEJS 服务器加载我的 HTML 和 CSS 文件? - How do I load my HTML and CSS file with my NODEJS server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM