我正在节点和我的onRequest监听器中构建一个超级简单的服务器我正在尝试确定是否应该根据路径提供静态文件(磁盘外)或某些json(可能是从mongo中提取)request.url.
目前我正在尝试首先统计文件(因为我在其他地方使用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
}
});
Run Code Online (Sandbox Code Playgroud)
除了cacheing的结果fs.stat为request.url.pathname,有没有东西可以加速这一检查呢?例如,是否可以快速查看是否fs.readFile出错而不是stat?或者用fs.createReadStream而不是fs.readFile?或者我可以使用某些东西来检查文件child_process.spawn吗?基本上我只是想确保在将请求发送到mongo以获取数据时,我不会花费任何额外的时间搞乱/ fileio ...
谢谢!
我想要做这样的工作:
var Events=require('events'),
test=new Events.EventEmitter,
scope={
prop:true
};
test.on('event',function() {
console.log(this.prop===true);//would log true
});
test.emit.call(scope,'event');
Run Code Online (Sandbox Code Playgroud)
但是,不幸的是,听众甚至没有被召唤.有没有办法做这个W/EventEmitter?我可以Function.bind听听,但是,我真的希望EventEmitter有一些特殊的(或明显的)方法来做到这一点......
谢谢您的帮助!
我在Node v0.5.9上运行以下命令:
var fs = require("fs");
fs.watch("/Users/username/testingFsWatcher/",function(event,file) {
console.dir(arguments);
});
Run Code Online (Sandbox Code Playgroud)
然后我做:
cd /Users/username/testingFsWatcher/>file1 - > { '0': 'rename', '1': null }mkdir new_folder - > { '0': 'rename', '1': null }>new_folder/file2 - >没有事件触发/输出touch file1 - >没有事件触发/输出rm file1 - > { '0': 'rename', '1': null }我注意到的两件事似乎不正确:子目录(new_folder)没有被监视,对监视文件的更新/修改不会触发change事件.查看节点代码并测试它们似乎应该是可能的.
那么,是否可以fs.watch观看目录及其所有子目录?是否至少可以从文件修改中获取事件?另外,从我所知道的,fs.watch支持kqueue哪一个eventid(在OSX中),是否有可能得到它eventid?
注意:我是专门使用fs.watch而不是fs.watchFile因为我需要观看整个目录(最好是所有的子目录:).
谢谢您的帮助!
我正在尝试使用Google Closure Compiler根据将要运行的位置(在服务器上与客户端)通过单个变量来分割我的应用程序代码.在这个例子中,将要在服务器上调用的所有内容都在isServerSidevar之后,但是,正在为客户端编译代码.所以我将设置isServerSide为false并让编译器删除客户端不会运行的所有内容...
里面app.js:
goog.provide('my.app');
my.app.log = function(message) {
document.write(message);
}
my.app.initClientSide = function() {
my.app.log('hello client');
}
my.app.initServerSide = function() {
my.app.log('hello server');
}
if (isServerSide) {
my.app.log('initing server');
my.app.initServerSide()
}else my.app.initClientSide();
Run Code Online (Sandbox Code Playgroud)
里面externs.js:
/**
* @define {boolean} is server side?
*/
var isServerSide=false;
Run Code Online (Sandbox Code Playgroud)
命令:
java -jar bin/compiler.jar --js closure-library/closure/goog/base.js --js app.js --externs externs.js --manage_closure_dependencies true --process_closure_primitives true --summary_detail_level 3 --warning_level VERBOSE --compilation_level=ADVANCED_OPTIMIZATIONS --closure_entry_point my.app
Run Code Online (Sandbox Code Playgroud)
预期产量:
document.write("hello client");
Run Code Online (Sandbox Code Playgroud)
实际产量:
isServerSide?(document.write("initing server"),document.write("hello server")):document.write("hello …Run Code Online (Sandbox Code Playgroud) javascript google-closure google-closure-library google-closure-compiler