首页 > Web开发 > 详细

NodeJs基础

时间:2019-08-22 12:53:05      阅读:81      评论:0      收藏:0      [点我收藏+]

一、什么是NodeJs?

  NodeJs是一个基于Chrome的v8引擎的JAVAScript运行环境;

  使用了事件驱动以及非阻塞的I/O式模型;

  NodeJs中的npm是全球最大的包管理网站。

二、引擎:

  脚本引擎(v8引擎)

  渲染引擎

三、I/O模型:

  I:input  输入

  O:output  输出

四、npm:是一个命令,也是一个网站,也是一个包

  npm一些常用指令:

  npm install:安装package.json中的依赖

  npm install 包名 --save:生产环境的依赖

  npm install 包名 --save-dev:开发环境的依赖

  npm install 包名 -g:全局安装

  npm update 包名 :升级

  npm uninstall 包名:卸载包

  npm cache clean :清除缓存

五、yarn(推荐使用)

  也是一个包管理器

  cnpm install yarn -g:安装yarn

  cnpm install jquery swiper layui zepto touchjs -S

  yarn add jquery swiper layui zepto touchjs

  yarn好处:安装的过程是异步安装,有缓存,下次安装的时候会缓存中进行安装

  npm init ===yarn init:让一个文件夹变成一个node包

  常用命令:

  yarn install:安装package.json中的依赖;

  yarn add 包名:生产环境的依赖;

  yarn add 包名 --dev :开发环境的依赖;

  yarn add 包名 -g:全局安装;

  yarn upgrade 包名:升级;

  yarn remove 包名:卸载

六、NodeJs与原生Js的区别:

  1、原生Js 中有BOM、DOM,但是NodeJs是运行在服务端的js环境,因此没有BOM、DOM、跨域的概念;

  2、NodeJs中也有原生Js中没有的属性,例如:process、global

  3、NodeJs可以调用底层的API、文件的读写、服务器的创建、环境变量的配置、连接/操作数据库......

  因此我们可以通过以上信息来判断当前环境是浏览器环境还是NodeJs环境

七、进入Node的交互模式:node 回车

  退出Node的交互模式:两次Ctrl+c || Ctrl+d

八、NodeJs能做什么事情?

  实际项目中做中间层,优点是解决高并发,缺点是不适合大量计算

九、NodeJs中的模块分为三部分:

  1、内置模块:http

  2、第三方模块:需要通过npm进行下载

  3、自定义模块:自己编写的模块

十、node创建服务器

const http = require("http");
//创建服务器
const server = http.createServer((req,res)=>{
    // res.setHeader("content-type","text/plain;charset=utf8")
    // res.statusCode = 200;
    console.log(req.url);
    console.log(req.headers)
    console.log(req.method);
    res.writeHead(200,{"content-type":"text/plain"})
    res.write("123");
    res.end("再见");
})
server.listen(9000,()=>{
    console.log("服务器启动:localhost:9000")
});
/*
req、res中的参数
1、req:request  请求
        (1)req.url  请求的url地址
        (2)req.headers 请求头
                 req.headers请求头
                    常用请求头的类型:
                    application/json
                    application/x-www-form-urlencoded    
        (3)req.method

2、res:response  响应
        (1)res.end      最后一次响应  只能调用一次
        (2)res.write    响应  可以调用多次
        (3)res.statusCode  设置服务器状态码
                setHeader:设置响应头    
                    常见的响应头类型:
                        text/plain
                        text/html
                        text/css
                        application/x-javascript
                        application/json
        (4)res.writeHead()   statusCode + setHeader的结合
                res.writeHead(200,{
                    "content-type":"text/html;charset=utf-8",
                    "Access-Control-Allow-Origin":"*"
                });
        (5)res.setHeader();  设置响应头

        $.ajax({
            type:"post",
            url:"",
            headers{
                content-type:"application/x-www-form-urlencoded"  数据序列化  
            }
            data:username=123&password=456
        })
        username=123&password=456
*/

十一、node连接服务器

const http = require("http");
let url = "http://m.maoyan.com/ajax/movieOnInfoList?token="

/*
    node服务器与服务器之间进行数据请求的时候数据是一块一块接收的  不像浏览器 一下子全部接收
*/

http.get(url,(res)=>{
    let str = ""
    res.on("data",(data)=>{
        str += data;
    })


    res.on("end",()=>{
        console.log(JSON.parse(str));
    })
})

 

  

NodeJs基础

原文:https://www.cnblogs.com/zgray110/p/11393391.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!