node的安装
- 安装包安装
- 官网下载相应的安装包
- nvm安装
- n(node)v(version)m(manager),称为node版本管理工具
- 因为以后的开发工作可能会在多个node版本中测试,而且node的版本也比较多,所以需要安装nvm来管理
- nvm下载地址链接
- nvm基本命令
nvm -v 查看版本号命令
nvm list 查看列表命令
nvm install 版本号 安装node命令
nvm uninstall 版本号 卸载node命令
相关版本
- node版本常识
- 偶数版本为稳定版 (0.6.x,0.8.x,0.10.x)
- 奇数版本为非稳定版 (0.7.x,0.9.x,0.11.x)
- 操作方式
- 重新下载最新的安装包,覆盖安装即可
Windows下常用的命令行操作
- 切换当前目录(change directory):cd
- 创建目录(make directory):mkdir
- 查看当前目录列表(directory):dir
- 别名:ls(list)
- 清空当前控制台:cls
- 别名:clear
- 删除文件:del
- 别名:rm
注意:所有别名必须在新版本的powershell(Linux系统)中使用
模块 包 commonjs
commonjs规范
- 前端模块化:AMD,CMD,Commonjs
- Node 应用由模块组成,采用commonjs模块规范
定义module
- 每个文件就是一个模块,有自己的作用域。在一个文件里面定义的变量、函数、类,都是私有的,对其他文件不可见。
暴露接口
- CommonJS规范规定,每个模块内部,module变量代表当前模块。这个变量是一个对象,它的exports属性(即module.exports)是对外的接口与。加载某个模块,其实是加载该模块的module.exports属性。
var x = 1;
var addX = function(value){
return value + x;
};
module.exports.x = x;
module.exports.addX = addX;
引用
require方法用于加载模块
var example = require('./example.js');
console.log(example.x);
console.log(example.addX(1));
模块的分类
- 内置模块
const process = require('process')
const path = require('path')
console.log(process.version)
console.log(path.resolve('../'))
- 第三方模块
const request=require("request");
console.log(request)
request.get('http://api.douban.com/v2/movie/in_theaters', (err, response, body) => {
if (!err) {
// console.log(body);
console.log(JSON.parse(body))
} else {
console.log(err);
}
})
- 自定义模块
npm使用入门
npm下载官网
安装:无需安装
查看当前版本:
npm -v
更新:
npm install npm@latest -g
初始化工程:
npm init
npm init --yes 默认配置
- 安装包
使用npm install会读取package.json文件来安装模块。安装的模块分为两类dependencies和devDependencies,分别对应生产环境需要的安装包和开发环境需要的安装包
更新模块:npm install npm install <package_name> npm install <package_name> --save npm install <package_name> --save-dev
卸载模块:npm updatenpm uninstall <package_name> npm uninstall --save lodash配置npm源
- 临时使用,安装包的时候通过–registry参数即可
npm install express --registry https://registry.npm.taobao.org- 全局使用
npm config set registry https://registry.npm.taobao.org
//配置后可通过下面方式来验证是否成功
npm config get registry
//或
npm info express
- cnpm使用
//安装cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
//使用cnpm安装包
cnpm install express
常用的内置模块
node常用内置api
-
URL网址解析
解析url相关网址信息
url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
url.format(urlObject)
url.resolve(from, to) -
QueryString 参数处理
querystring.escape(str)
querystring.unescape(str)
querystring.parse(str[, sep[, eq[, options]]])
querystring.stringify(obj[, sep[, eq[, options]]]) -
HTTP 模块概要
http.createServer([options][, requestListener])
http.get(options[, callback]) -
事件 events 模块
-
文件fs模块
-
Stream 流模块
-
request 方法
这篇博客介绍了Node.js的基础知识,包括如何在Windows环境下安装Node.js,理解不同版本的区别,以及常用命令行操作。重点讲解了CommonJS模块规范,包括模块的定义、暴露接口和引用方法。还提到了npm的使用,如安装、更新、卸载模块,以及一些常用的内置模块如HTTP、URL和Querystring。
5917

被折叠的 条评论
为什么被折叠?



