nodejs形成的数据被覆盖

我试图让每个HTML表单提交创build它自己的时间戳文件,但当表单提交第二次时,它被覆盖与新的时间戳。 我应该把这个循环?

app.use(bodyParser.urlencoded({ extended: true })); app.post('/myaction', function(req, res) { res.send('Thank you for your inquiry, someone will contact you shorty.'); fs.writeFile(timeEntry+'submission.txt', req.body.businessname, function(err) { if (!err) { console.log('Wrote data to file.txt'); } else { throw err; } }); fs.appendFile(timeEntry+'submission.txt', req.body.contactname, function(err) { if (!err) { console.log('Wrote data to file.txt'); } else { throw err; } });` 

我不知道你在timeEntry定义了timeEntry但是你需要把它的声明移到app.post()块中,这样app.post()在请求时重新计算(否则,每个请求的值都是一样的)

以下应该工作:

 app.use(bodyParser.urlencoded({ extended: true })); app.post('/myaction', function(req, res) { res.send('Thank you for your inquiry, someone will contact you shorty.'); var timeEntry = Date.now(); fs.writeFile(timeEntry +'submission.txt', req.body.businessname, function(err) { if (!err) { console.log('Wrote data to file.txt'); } else { throw err; } }); });