鍍金池/ 問答/HTML/ node中 自制模塊獲取時間戳不能更新

node中 自制模塊獲取時間戳不能更新

問題

被require的模塊里 過去時間戳, 第二次也獲取了和第一次相同的時間戳~~~

代碼

前面代碼正常, 先只給出出問題的地方, 大佬需要我再貼全部吧

a.js 使用express router

let module = require('./b.js');
router.post('/api',(req,res)=>{
    b.main(()>{
        req.status(200).json({})
    })
})

b.js

function main(){
    if(true){
        let cmodule = require('./c.js');
        cmodule.main()
    }
}
exports.main = main

c.js

var fs = require('fs'),
    timestamp = new Date().getTime(),
    dhh = fs.createWriteStream('./aa'+timestamp+'.txt');
function main(){
    console.log(timestamp)
    if(當(dāng)滿足條件){
        dhh.end("Done"); // 將可寫流關(guān)閉, 變成為不可寫狀態(tài)
        return;
    }
    stream = fs.createReadStream('./aa.txt');
    stream.pipe(dhh, {end: false});
    stream.on("end", function() {
        main();        
    });
}
exports.main = main

index.html

ajax.post '/api'

控制臺

第一次 console.log(timestamp) 1526885307155

第二次 console.log(timestamp) 1526885307155
結(jié)果一樣,不知原因~求指教
回答
編輯回答
爆扎

你的c模塊一加載就已經(jīng)執(zhí)行了,后面都是取得執(zhí)行后的結(jié)果

2018年3月20日 12:10