Node.js基本讲解

什么是Node.js?

简单的说 Node.js 就是运行在服务端的 JavaScript。Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。

Node.js有两种模式,一种是脚本模式,一种是交互模式。

Node.js框架,比较热门的有 express,koa,hapi。其中express是最热门的框架,用的比较多,文档比较全,较稳定,而且很全面。koa太过于超前,不是很稳定,正在完善中。hapi框架太过于复杂,适合超大型的项目,做一个简单的程序就需要大量的环境堆砌。

http是nodejs里边自带的一个模块,可以用require方法导入,比如:

1 var http = require("http");

现在我们就已经用http创建了一个web服务,http中自带一个function叫createServer,里边有两个参数,request和response,request是浏览器发送给服务器的请求,response是服务器对浏览器的响应。

 1 http.createServer(function(request,response){
 2     if (request.url!=="/favicon.ico") { //清除浏览器的第二次访问
 3         //200代表的是http的状态:OK
 4         response.writeHead(200,{"content-Type":"text/html;charset = utf-8"});
 5         //对前端的响应
 6         response.write("你好");
 7         console.log("被访问");
 8         response.end();        
 9     }
10 }).listen(8000);//监听8000端口(本地端口)

这里用的是苹果电脑,接下来是用终端来操作。首先在终端中通过cd回到主要菜单,ls查看里边包括的文件夹,cd打开对应的文件夹,这样一级一级打开,找到我们创建的文件,之后输入node + 文件名。之后在浏览器中打开localhost:8000,就可以访问自己写的js了。

调用函数的代码:

 1 var http = require("http");
 2 http.createServer(function(request,response){
 3     if (request.url!=="/favion.ico") {
 4         response.writeHead(200,{"content-Type":"text/html;charset = utf-8"});
 5         eat(response,"饺子");
 6         response.end();
 7     }
 8 }).listen(8000);
 9 console.log("服务器在本地的8000端口上");
10 function eat(res,food){
11     res.write(food);
12 }

node.js里边的类,包含属性和函数,继承关系。我们新建一个文件,里边内容是:

1 function User(id,name){
2     this.id = id;
3     this.name = name;
4     this.eat = function(){
5         return "吃东西";
6     }
7 }

但是我们想让外部使用必须想办法,我们有俩中办法:

第一种方法:global.waring是一种,全局文件都可以用但是node.js不推荐使用;

第二种方法:node.js中每个模块中都有一个属性叫modulemodule代表模块本身,里边有一个属性是exports,可以导出。例如:

1 module.exports = User;

接下来是引入这个文件

1 var temp = require("./node_03.js");

在网页上显示“吃东西”。

1 var http = require("http");
2 http.createServer(function(request,response){
3     if (response.url!=="/favion.ico") {
4         response.writeHead(200,{"content-Type":"text/html;charset = utf-8"});
5         var node = new temp();
6         response.write(node.eat());
7         response.end();
8     }
9 }).listen(8000);

我们看看module的属性都有哪些:

  1. id:模块的标识符,会加上带有他的绝对路径的文件名
  2. exports:整个模块要对外输出的值
  3. parent:调用当前模块的模块
  4. filename:文件的名字
  5. loaded:文件是否加载
  6. children:引用了哪个文件
  7. path:路径

http是一个模块,这个模块中有一个属性是STATUS_CODES是一个对象,里边的各种属性的值都是状态吗,属性的值是每个状态的解释。

request中的属性有4个:

  1. url:发送请求的网站
  2. method:请求的方式
  3. headers: http请求的所有的http头部信息
  4. setEcoding:可以设置请求的编码个是

post 的request-> data 和 end 俩个事件  可以设立对这俩个事件的监听函数

处理post请求:

1 var content = "哈哈";
2 request.on("data",function(a){
3    content += a; 
4 });
5 request.on("end",function(){
6    response.writeHead(200,{"Content-Type":"text/html;charset = utf-8"}) ;
7    response.write("你发送了"+content);
8    response.end(); 
9 })

接下来这段代码用于生成https协议密匙,我们在终端里边这样写:

1 openssl genrsa -out key.em
2 openssl req -new -key key.pem -out csr.pem
3 openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.nem 
4 rm csr.pem

这是在终端的样子:

可能会无法创建这个不要紧,只要从网上再搜一些这方面的代码就行。

 1 var https = require("https");
 2 var fs = require("fs");
 3 
 4 var options = {
 5     key:fs.readFileSync("key.pem"),
 6     cert:fs.readFileSync("cert.pem"),
 7 };
 8 
 9 var server = https.createServer(options,function(request,response){
10     response.writeHead(200,{"Content-Type":"text/html;charset = utf-8"});
11     response.end("hello world");
12 }).listen(8000);

node内置的https支持

1 var server = https.createServer({
2     key:privateKey,
3     cert:certificate,
4     ca:certificateAuthoritycertificate
5 },app);
原文地址:https://www.cnblogs.com/hgs-159/p/6052225.html