2016-05-19 3 views
4

モジュールの初期化時に存在しないファイルを作成するアプリケーション用のログモジュールを作成します。 (モジュール内のinit関数が呼び出されます)。nodejs fs.openSync()でフラグ 'a'を指定すると、存在しないファイルが作成されません。

しかし、fs.openSync(log_file_name,'a')で新しいログファイルを作成しようとすると、常にエラーが発生します。

新しいファイルを作成できない理由を教えていただいた方に感謝します。

const fs=require('fs'); 
const path=require('path'); 
const moment=require('moment'); 
var time=function() 
{ 
    return moment().format('YYYY-MM-DD[_]hh:mm:ss:SSS'); 
}; 

//init 
var fd; // file descriptor 

function init(log_dir) 
{ 
    var log_file_name=path.join(log_dir,time()+'.log'); 
    this.fd=fs.openSync(log_file_name,'a'); 
} 
init(__dirname); 

エラーは以下のようなものです:

"C:\Program Files (x86)\JetBrains\PhpStorm 2016.1\bin\runnerw.exe"                                                                             "C:\Program Files\nodejs\node.exe" index.js 
fs.js:634 
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode); 
      ^

Error: ENOENT: no such file or directory, open 'I:\twesix\lib\twesix_nodejs\log\2016-05-19_01:34:52:621.log' 
at Error (native) 
at Object.fs.openSync (fs.js:634:18) 
at init (I:\twesix\lib\twesix_nodejs\log\index.js:15:16) 
at Object.<anonymous> (I:\twesix\lib\twesix_nodejs\log\index.js:24:1) 
at Module._compile (module.js:541:32) 
at Object.Module._extensions..js (module.js:550:10) 
at Module.load (module.js:456:32) 
at tryModuleLoad (module.js:415:12) 
at Function.Module._load (module.js:407:3) 
at Function.Module.runMain (module.js:575:10) 

Process finished with exit code 1 

私は窓10と私のNode.jsのバージョンを使用しています6.1.0

+0

は、あなたが時間()+に有効な値を持っている「ログイン」? –

+0

console.log(log_file_name)は、I:¥twesix¥lib¥twesix_nodejs¥log¥2016-05-19_01:52:28:776.logになります。このファイル名に無効な文字などが含まれていますか? – twesix

+0

そうですが、コロンはウィンドウ上のファイル名には有効ではありません。私はちょうど答えを書いた –

答えて

3

問題がmoment().format('YYYY-MM-DD[_]hh:mm:ss:SSS');によって返された文字列が含まれていることですコロン(:)は、Windows上のファイル名には使用できません。

の変更と同様に、Windowsのファイル名に無効な文字が含まれていない何かにformat()

return moment().format('YYYY-MM-DD[_]hh-mm-ss-SSS'); 
+1

ああ、それは動作します!ありがとう、たくさんありました。素敵な一日を! – twesix

関連する問題