首页 > 解决方案 > 使用 fs.appendFile 打印的节点 js 未定义

问题描述

我刚开始学习 Node js。我不明白为什么在 output.txt 中我在调用时在 output.txt 中看到“2020 Septemberundefined undefined”:

http://localhost/?month=September&year=2020

我希望在 output.txt 中只看到“2020 年 9 月”。

var http = require('http');
var url = require('url');
var fs = require('fs');

//create a server object:
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  var q = url.parse(req.url, true).query;
  var txt = q.year + " " + q.month;
  fs.appendFile('output.txt', txt, function (err) {
        // nothing
  });
  res.end(); //end the respons
}).listen(8080); //the server object listens on port 8080

标签: node.js

解决方案


最喜欢的图标。添加console.log(req.url). 您将看到浏览器发出两个请求。


推荐阅读