在node . js中检查文件是否存在的最快方法

时间:2022-11-25 16:53:32

I'm building a super simple server in node and in my onRequest listener I'm trying to determine if I should serve a static file (off the disk) or some json (probably pulled from mongo) based on the path in request.url.

我正在node中构建一个超级简单的服务器,在我的onRequest监听器中,我试图确定我应该根据request.url中的路径提供静态文件(磁盘外)还是json(可能来自mongo)。

Currently I'm trying to stat the file first (because I use mtime elsewhere) and if that doesn't fail then I read the contents from disk. Something like this:

目前我正在尝试将文件放在首位(因为我在其他地方使用mtime),如果没有失败,那么我将从磁盘读取内容。是这样的:

fs.stat(request.url.pathname, function(err, stat) {
    if (!err) {
        fs.readFile(request.url.pathname, function( err, contents) {
            //serve file
        });
    }else {
        //either pull data from mongo or serve 404 error
    }
});

Other than cacheing the result of fs.stat for the request.url.pathname, is there something that could speed this check up? For example, would it be just as fast to see if fs.readFile errors out instead of the stat? Or using fs.createReadStream instead of fs.readFile? Or could I potentially check for the file using something in child_process.spawn? Basically I just want to make sure I'm not spending any extra time messing w/ fileio when the request should be sent to mongo for data...

除了缓存fs的结果之外。request.url统计。路径名,有什么东西能加速这个检查吗?例如,它是否就像看fs一样快。读出错误而不是统计?或者使用fs。createReadStream代替fs.readFile呢?或者我可以使用child_process.spawn中的一些东西来检查文件吗?基本上,我只是想确保,当请求发送到mongo数据时,我不会花费任何额外的时间来干扰w/ fileio。

Thanks!

谢谢!

3 个解决方案

#1


56  

var fs = require('fs');

fs.exists(file, function(exists) {
  if (exists) {
    // serve file
  } else {
    // mongodb
  }
});

#2


2  

I don't think you should be worrying about that, but rather how can you improve the caching mechanism. fs.stat is really ok for file checking, doing that in another child process would probably slow you down rather then help you here.

我认为您不应该为此担心,而是应该如何改进缓存机制。fs。stat确实适合进行文件检查,在另一个子进程中这样做可能会减慢您的速度,而不是在这里帮助您。

Connect implemented the staticCache() middleware a few months ago, as described in this blog post: http://tjholowaychuk.com/post/9682643240/connect-1-7-0-fast-static-file-memory-cache-and-more

Connect几个月前实现了staticCache()中间件,如本文所述:http://tjholowaychuk.com/post/9682643240/connect-1-7-快速静态文件-内存-cache-等等

A Least-Recently-Used (LRU) cache algo is implemented through the Cache object, simply rotating cache objects as they are hit. This means that increasingly popular objects maintain their positions while others get shoved out of the stack and garbage collected.

通过缓存对象实现最近最少使用(LRU)缓存algo,只需在缓存对象被命中时旋转它们。这意味着,越来越流行的对象保持它们的位置,而其他对象则被从堆栈中推出并收集垃圾。

Other resources:
http://senchalabs.github.com/connect/middleware-staticCache.html
The source code for staticCache

其他资源:http://senchalabs.github.com/connect/middlewar-staticcache.html staticCache的源代码

#3


2  

this snippet can help you

这个片段可以帮助您

fs = require('fs') ;
var path = 'sth' ;
fs.stat(path, function(err, stat) {
    if (err) {
        if ('ENOENT' == err.code) {
            //file did'nt exist so for example send 404 to client
        } else {
            //it is a server error so for example send 500 to client
        }
    } else {
        //every thing was ok so for example you can read it and send it to client
    }
} );

#1


56  

var fs = require('fs');

fs.exists(file, function(exists) {
  if (exists) {
    // serve file
  } else {
    // mongodb
  }
});

#2


2  

I don't think you should be worrying about that, but rather how can you improve the caching mechanism. fs.stat is really ok for file checking, doing that in another child process would probably slow you down rather then help you here.

我认为您不应该为此担心,而是应该如何改进缓存机制。fs。stat确实适合进行文件检查,在另一个子进程中这样做可能会减慢您的速度,而不是在这里帮助您。

Connect implemented the staticCache() middleware a few months ago, as described in this blog post: http://tjholowaychuk.com/post/9682643240/connect-1-7-0-fast-static-file-memory-cache-and-more

Connect几个月前实现了staticCache()中间件,如本文所述:http://tjholowaychuk.com/post/9682643240/connect-1-7-快速静态文件-内存-cache-等等

A Least-Recently-Used (LRU) cache algo is implemented through the Cache object, simply rotating cache objects as they are hit. This means that increasingly popular objects maintain their positions while others get shoved out of the stack and garbage collected.

通过缓存对象实现最近最少使用(LRU)缓存algo,只需在缓存对象被命中时旋转它们。这意味着,越来越流行的对象保持它们的位置,而其他对象则被从堆栈中推出并收集垃圾。

Other resources:
http://senchalabs.github.com/connect/middleware-staticCache.html
The source code for staticCache

其他资源:http://senchalabs.github.com/connect/middlewar-staticcache.html staticCache的源代码

#3


2  

this snippet can help you

这个片段可以帮助您

fs = require('fs') ;
var path = 'sth' ;
fs.stat(path, function(err, stat) {
    if (err) {
        if ('ENOENT' == err.code) {
            //file did'nt exist so for example send 404 to client
        } else {
            //it is a server error so for example send 500 to client
        }
    } else {
        //every thing was ok so for example you can read it and send it to client
    }
} );