[英]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.