node.js 基础篇
2024-09-24 00:07:22
日志输出方式
node test.js 2>error.log 1>info.log
如果需要日志文件追加 node test.js 2>>error.log 1>>info.log
如果是用 sublimeText-Nodejs 需要在 Nodejs.sublime-build 中修改以下节点(根据自己的操作系统)
"cmd": ["taskkill /F /IM node.exe & node $file 2>>error.log 1>>info.log", ""]
如果不设置,默认输出到系统console
日志语法
console.log('Server running at http://127.0.0.1:8888/');
console.info('text: %s !', message);
console.error('this is a error');
console.warn('this is a warn');
node.js中日志中无法区分warn或者error,统一保存在异常日志中
输出某段代码执行时间
console.time("hi");
console.log("it works!");
console.timeEnd("hi");
http
一个简单的http服务
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('Hello World\n');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
一个简单的http客户端
http.get({
hostname: 'localhost',
port: 8888,
path: '/',
agent: false // create a new agent just for this one request
}, function (res) {
var data = '';
res.on('data', function (chunk){
data += chunk.toString();
});
res.on('end',function (){
console.log("data is:"+data);
});
});
http.get('http://localhost:8888',function (res) {
var data = '';
res.on('data', function (chunk){
data += chunk.toString();
});
res.on('end',function (){
console.log("data is:"+data);
});
});
最新文章
- [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字
- http 协议集合,超级简单
- [原创]PCB知识补充
- PHP实现微信公众账号开发
- 用wcf实现带有“秒传”功能的网盘
- 防范 DDoS 攻击的 15 个方法
- C# 里窗体里(windows form)怎么播放音乐
- PTA的使用简介
- Python可视化TVTK库初使用
- Java初学习-常见单词
- 【C++】const &; 指针
- Luogu P3239 [HNOI2015]亚瑟王
- 一些优秀的Python包
- Confluence 6 从其他备份中恢复数据
- 推荐一本写给IT项目经理的好书
- Java容器解析系列(6) Queue Deque AbstractQueue 详解
- day_5.22 py
- 通过脚本命令cacls提升某个用户都某路径的操作权限
- 『ACM C++』 PTA 天梯赛练习集L1 | 048-49
- HBase Scan,Get用法