美文网首页
node初步学习

node初步学习

作者: codingQi | 来源:发表于2019-12-14 18:14 被阅读0次

    一、node的作用和应用

    • 脱离浏览器运行js
    • 后台API编写
    • Wepback、Gulp、Npm等等都是强依赖node的
    • 中间层:服务器中负责IO读写的中间层服务器(比如:文件的读写、数据库的查询等都是中间层做比较好)

    什么是中间层、作用:

    • 性能好
      (1)异步IO:适合处理高并发的请求
      (2)也可以做一些需要缓存的事情
    • 帮忙处理数据:得到前端想要的数据类型
    • 提高安全性:遇到web攻击时,多一层保护

    二、Node的优势

    • 便于前端开发入门(都是基于js)
    • 性能高
    • 利于前端代码整合(可以共用模块)

    三、cnpm淘宝镜像

    npm install -g cnpm --registry=https://registry.npm.taobao.org

    四、node中的模块

    • 全局模块(定义:何时何地都能访问,不需要引用)
      process.env -(环境变量)
      process.argv

      console.log(process.argv)----运行node hello.js
      console.log(process.argv)----运行node hello.js 1 2 3 4
    • 系统模块(定义: 需要require(),但不需要单独下载)
      path - (用于处理文件路径和目录路径的实用工具)

    let path  = require('path');
    
    console.log(path.dirname('/node/a/b/c/1.jpg')); // 文件目录
    console.log(path.basename('/node/a/b/c/1.jpg')); // 文件名
    console.log(path.extname('/node/a/b/c/1.jpg')); // 扩展名
    
    console.log(path.resolve('/node/a/b/c', '../../', 'd'));
    console.log(path.resolve(__dirname, 'index.js'));
    

    fs - (用于文件的读写操作)

    let fs = require('fs');
    
    // 异步读取文件
    fs.readFile('./a.txt', (err, data) => {
        if (err) {
            console.log(err);
        } else {
            console.log(data); // <Buffer 61 62 63>
            console.log(data.toString());  // abc
        }
    })
    
    let fs = require('fs');
    
    // 异步写文件(1)
    fs.writeFile('b.txt', '月薪2元', (err) => {
        if (err) {
            throw err;
        }
    })
    
    // 异步写文件(2)-------  { flag: 'a' }代表追加 
    fs.writeFile('b.txt', '月薪2元哈哈哈', { flag: 'a' } , (err) => {
        if (err) {
            throw err;
        }
    })
    
    (1)
    (2)

    同步的话不写回调就行了,如:fs.readFileSync('a.txt')fs.writeFileSync('b.txt', '月薪2元'),只不过node经常用的是异步的而已。

    • 自定义模块(定义:require自己封装的模块)
      exports:
    // mod.js
    exports.a = 1;
    exports.b = 2;
    
    // 其他文件使用
    let mod = require('mod');
    console.log(mod.a);
    console.log(mod.b);
    

    module:

    // mod.js
    (1)
    module.exports = {
      a: 1,
      b: 2
    }
    (2)
    module.exports = function() {
      console.log('我是一个function');
    }
    (3)
    module.exports = class {
      constructor() {
        this.state = {
          data: '我是一个class'
        }
      }
      show() {
        console.log(this.state.data);
      }
    }
    
    
    
    // 其他文件使用
    (1)
    let mod = require('./mod');
    console.log('a==', mod.a, '\n', 'b==', mod.b);
    // 结果:
    // a== 1
    //  b== 2
    
    (2)
    let mod = require('./mod');
    mod();
    // 结果:
    // 我是一个function
    (3)
    let Mod = require('./mod');
    let mod = new Mod()
    mod.show();
    // 结果:
    // 我是一个class
    

    require:
    (1)如果有路径,就去路径里面找;
    (2)没有的话就去node_modules里面找;
    (3)都没有的话再去node的安装目录里面找。

    • http模块
      node通常是作为一个服务器,更多的是作为一个web服务器。
      重点:http.createServer((req, res) => { // })
    • GET请求:
      (1)放在请求头里(url?xx=xx&xx=xx);
      (2)< 32K;
      (3)url模块进行解析。
      url.parse(req.url):将url和query拆分开来;
      url.parse(req.url, true):将query解析为json格式;
    • POST请求:
      (1)放在请求体里面;
      (2)< 2G;
      (3)querystring帮助解析为json格式。
    http.createServer((req, res) => {
        let result = [];
        req.on('data', buffer => { // 二进制格式,数据大了的话就会分为一条一条的执行很多次‘data’,从而就有很多个buffer。
            result.push(buffer); // 将所有的buffer放在一起,后面获取。
        })
        req.on('end', () => {
            let data = Buffer.concat(result).toString(); // username=admin&password=1234
            console.log(querystring.parse(data)); // [Object: null prototype] { username: 'admin', password: '1234' }
        })
    }).listen(8080);
    

    真实情况不会这样用,只是了解一下。

    五、【登录注册功能】实战

    实战代码已放在github上,查阅请点击这里

    相关文章

      网友评论

          本文标题:node初步学习

          本文链接:https://www.haomeiwen.com/subject/zbitnctx.html