• node笔记及实践代码


    1、express static实现静态资源

    // 这里app.use是一个中间件,所谓中间件其实也是一个路由,
    // 是一个可以处理所有http请求的路由
    //相当于访问一个绝对路径
    第一种 直接访问public
    app.use(express.static(path.join(__dirname, '/public')))
    第二种 以/public访问public
    app.use('/public', express.static(path.join(__dirname, '/public')))
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、path

    process.cwd()返回当前工作目录。如:调用node命令执行脚本时的目录。
    
    当前文件的绝对路径 __filename
    当前文件夹的绝对路径 __dirname 
    拼接路径path.join(__dirname,'index.html')
    
    返回path的目录名
    path.dirname('/目录1/目录2/目录3');
    // 返回: '/目录1/目录2'
    
    返回扩展名
    path.extname('index.html');
    // 返回: '.html'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3、url路径解析模块

    //url模块
    var urlObj = url.parse(req.url, true);
    var query = urlObj.query;
    
    //querystring模块
    querystring.stringify()
    querystring.parse();
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、params传参的形式

    //请求参数是虚拟的
    .get('/api/product/remove/:id', api.removeProduct);
    //前端ajax请求路径
    '/api/product/remove/'+ [变量]
    //后台 api.removeProduct接口获取参数
    则req.params 获取参数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5、表单提交

    //配置
    var bodyParser = require("body-parser");
    app.use(bodyParser.urlencoded({ extended: false }))
    app.use(bodyParser.json())
    //使用
    req.body;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6、获取请求信息

    req上面包含了所有的请求信息
    打印一下,找到你想要的
    
    • 1
    • 2
    const host = req.headers['x-forwarded-host'] || req.host;
    
    • 1
    	给浏览器种cookie,可以在控制台的cookie查看
         res.cookie('gpc', 'haha', {
              maxAge: 0,
          });
    
    • 1
    • 2
    • 3
    • 4
    const getIP = headers => {
      let ip = headers['x-real-ip'] || headers['ali-cdn-real-ip'];
      if (Array.isArray(ip)) {
        ip = ip[0];
      }
      if (!ip) {
        ip = headers['x-forwarded-for'];
        if (ip) {
          if (ip.includes(',') && typeof ip === 'string') {
            const ips = ip.split(/,\s*/);
            const rIsLocal = /^(10|192\.168)\./;
            if (ips.length > 1 && rIsLocal.test(ips[0]) && !rIsLocal.test(ips[1])) {
              ip = ips[1];
            }
            ip = ips[0];
          }
        }
      }
      return ip || '';
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    常见面试题-Netty专栏(一)
    如何搭建数据驱动自动化测试框架
    java 工程管理系统源码+项目说明+功能描述+前后端分离 + 二次开发
    随时随地时时刻刻使用GPT类应用
    浙大MBA考研经验分享:名校梦不可负~
    SD/SDIO(1):SD总线协议介绍
    秋招每日一题T28——最大连续子序列
    4、linux下文件的通用操作方法
    法线方程实现最小二乘拟合(Matlab)
    Go template详解(上)- 注释、作用域、空格和空行、管道、{{range .}}、{{with .}}(helm进阶语法)
  • 原文地址:https://blog.csdn.net/qq_36262295/article/details/126621226