nodejs形成被覆盖的数据

时间:2016-02-09 12:54:12

标签: html node.js

我正在尝试让每个html表单提交创建它自己的时间戳文件,但是当第二次提交表单时,它会被新的时间戳覆盖。我应该把它放在一个循环中吗?

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;
    }
  });`

1 个答案:

答案 0 :(得分:1)

我不确定你在timeEntry定义的位置,但是你要将其声明移到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;
    }
  });
});