NodeJs的安装
-
在 https://nodejs.org/ 官网中进行下载
# 不需要配置环境变量,安装过程中已经自动完成环境变量的安装。
# 什么叫做环境变量?
就是在系统的任何目录下,都能运行c:\program files\nodejs
里面的程序。 -
安装完成后 在windows环境下打开命令提示符
键入node -v
# 如果安装成功,会显示想对应的node版本号
键入npm -v
# 查看Node.js的包管理工具npm的版本工具 -
进入nodejs的repl交互环境
键入node
回车 # 代表进入了交互环境可以进行类似浏览器console控制台的操作
# 或者在nodejs的安装目录中找到nodejs.exe
双击打开也是进入了repl交互环境 -
退出nodejs
连续两次ctrl + c
即可
NodeJs端javascript和浏览器端的javascript的异同?
相同点:
基本语法、数据类型相同
# 声明变量、函数
# 数值、字符串、对象、数组、函数、布尔、undefined
# 不同的数据类型的属性和方法
不同点:
全局对象不同
# 在浏览器端 # window
# 在NodeJs端 # global
javascript的运行环境不同,所以两者的全局对象下的属性和方法基本上都是不同 (即window下的属性和方法在nodejs中没有,global中的属性和方法在浏览器端也是没有的)
Module(模块)
一般情况下一个模块代表是一个功能的集合,能够完成某个特定的功能,模块一般为对象或者是一个函数。
在NodeJs中模块分为系统模块和自定义模块
系统模块
# 也叫内置模块,NodeJs自己提供,不需要另外下载和安装
# fs模块、http模块、path模块、util模块等
自定义模块
# 需要自己定义封装,然后引入才能使用
# 外部的模块cheerio,这个模块可以让我们像jquery一样操作html代码
NodeJs中有大量的模块为什么不把这些模块融合在一起?
# 有一些程序只需要使用某些模块(如文件读取功能)来完成一个特定的事情,所以为了效率,完全没有必要引入其他的模块,需要用什么模块,就require()
这个模块。
NodeJs 内置模块 HTTP
#引入代码
var http = require("http");
1.如果想修改程序,必须ctrl + c
中断当前运行的服务器,重新node一次,刷新。
2.思考,网页如何通过服务器被访问到?
3.如何通过访问NodeJs服务器访问的是一个完整的html页面?
# 通过引入fs模块来对html文件进行读取,然后把读取到的数据输出到html当中
==============================================
--- http 服务端 ---
4.http.createServer(function(req, res) {}).listen(3000);
创建服务器
# req
是http.IncomingMessage
的实例,res
是http.SrverResponse
的实例
最简单的nodejs服务器已经搭建
var http=require("http");
http.createServer(function(req,res){
res.writeHead(200,{
"content-type":"text/plain"
});
res.write("hello nodejs");
res.end();
}).listen(3000);
同样可以这样写:
var http=require("http");
var server=new http.Server();
server.on("request",function(req,res){
res.writeHead(200,{
"content-type":"text/plain"
});
res.write("hello nodejs");
res.end();
});
server.listen(3000);
==============================================
--- http 客户端 ---
5.http.get('路径', callback);
发送get请求
6.http.request(options, callback);
发送请求
# options: options是一个类似关联数组的对象,表示请求的参数,callback作为回调函数,需要传递一个参数。options常用的参数post、port(默认为80)、method(默认为GET)、path(请求的相对于根的路径,默认是"/",其中querystring应该包含在其中,例如/search?query=xxxx)、headers(请求头内容)。
// get请求
var http=require("http");
var options={
hostname:"cn.bing.com",
port:80
}
var req=http.request(options,function(res){
res.setEncoding("utf-8");
res.on("data",function(chunk){
console.log(chunk.toString())
});
console.log(res.statusCode);
});
req.on("error",function(err){
console.log(err.message);
});
req.end();
// post请求
var http=require("http");
var querystring=require("querystring");
var postData=querystring.stringify({
"content":"我来测试一下",
"mid":8837
});
var options={
hostname:"www.imooc.com",
port:80,
path:"/course/document",
method:"POST",
headers:{
"Accept":"application/json, text/javascript, */*; q=0.01",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.8",
"Connection":"keep-alive",
"Content-Length":postData.length,
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Cookie":"imooc_uuid=6cc9e8d5-424a-4861-9f7d-9cbcfbe4c6ae; imooc_isnew_ct=1460873157; loginstate=1; apsid=IzZDJiMGU0OTMyNTE0ZGFhZDAzZDNhZTAyZDg2ZmQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjkyOTk0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGNmNmFhMmVhMTYwNzRmMjczNjdmZWUyNDg1ZTZkMGM1BwhXVwcIV1c%3DMD; PHPSESSID=thh4bfrl1t7qre9tr56m32tbv0; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1467635471,1467653719,1467654690,1467654957; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1467655022; imooc_isnew=2; cvde=577a9e57ce250-34",
"Host":"www.imooc.com",
"Origin":"http://www.imooc.com",
"Referer":"http://www.imooc.com/video/8837",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2763.0 Safari/537.36",
"X-Requested-With":"XMLHttpRequest",
}
}
var req=http.request(options,function(res){
res.on("data",function(chunk){
console.log(chunk);
});
res.on("end",function(){
console.log("评论完毕!");
});
console.log(res.statusCode);
});
req.on("error",function(err){
console.log(err.message);
})
req.write(postData);
req.end();
网友评论