nodejs 入门

1. hello word

hello.js

console.log("hello");

node hello.js即可

2.调试

如果 npm install太慢 可以使用国内淘宝的镜像

npm config set registry http://registry.npm.taobao.org 

npm -g install node-inspector

安装若出现如下错误,请安装visual studio

utf-8-validateuildinding.sln : error MSB3411: 未能加载 Visual C++ 组件“VCBuild.exe”。如果未安装该组件,请执行下列操作之一: 1) 安装 Microsoft Windows SDK for Windows Server 2008 和 .NET Framework 3.5;或 2) 安装 Microsoft Visual Studio 2008。

2015-11-11 10-37-13_node-pre-gyp

node-inspector命令启动调试器

再起一个命令行窗口执行命令

node debug 你程序.js

用chrome访问http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858

就可以调试了

支持用restart等命令操作调试过程

3. 判断一个路径是目录还是文件

var fs = require('fs');
var stats = fs.lstatSync(testPath);
if (stats.isFile()){
    // consolg.log(" is file");
}

4. 获取文件名 和 文件扩展名

path.extname(name);
path.basename(name, extName);

5.npm使用

5.1 列出当前模块的依赖

npm list

5.2 查看模块详情

npm show 模块名

5.3 查看模块版本:

npm info underscore version
5.4 安装制定版本的模块
npm install glob@4
注意: 同一个模块不同版本的安装
此时 就不要加-g全局参数了, 就安装你项目的本地就可以了
6. 依赖版本号表达式
http://www.cnblogs.com/xiyangbaixue/p/4123085.html
7. GBK编码处理
使用模块iconv-lite
fs.readFile(fullPathOfHtml, function (err,data) {
  if (err) {
    return console.log(err);
  }
  var str = iconv.decode(data, 'gbk');
  console.log(str);
});
8.cheerio输出中文全是unicode码,类似: 城商行
load的时候 增加 {decodeEntities: false}
var $ = cheerio.load(str, {decodeEntities: false});
EOF
 
 
原文地址:https://www.cnblogs.com/simoncook/p/4956225.html