node.js - 使用 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
解决方案
最喜欢的图标。添加console.log(req.url)
. 您将看到浏览器发出两个请求。
推荐阅读
- android - React Native Android getLaunchOptions 使用 Android Restrictions
- r - dplyr:使用lead()后在分组的data.frame中填充系列
- sql - 使用动态名称查询多个表的 U-SQL 作业
- sql - SQL 对列中具有相同数据的行进行分组
- r - 用户列表的转推计数
- python - python kivy garden mapview经纬度不更新
- excel - 用一个不带 OR 语句的语句查看多个值
- html - 在 jinja 循环 (Django) 中创建新的 html/css 元素的问题
- c - 在Linux下的实践中,信号驱动的I/O实际上是否被epoll弃用了?
- mongodb - CosmosDB MongoDB API 似乎没有使用自动创建的索引