Notes
  • 速记
  • 前端面试
  • HTML / CSS
    • HTML
    • CSS
    • CSS Modules
      • CSS Modules
        • CSS Modules Usage
      • Stylus
      • Nunjucks
  • Javascript
    • 正则表达式
    • 代理(Proxy)和反射(Reflection)
    • 类型转换
    • 按位操作
    • 数据可视化
    • 数据采集
      • 无(全)埋点
      • 模块曝光事件
    • package
      • axios
    • Event Loop
    • React
      • React热身
      • VDOM和DOM-diff
    • Vue
    • Omi
    • MVVM
    • 百度小程序
    • AST抽象语法树
    • ServiceWorker
    • WebSocket
  • NodeJS
    • Assert 断言
    • chai.js 断言库
    • Node Global Obj And Var
    • CLI Writed By Nodejs
    • Framework
      • Hapi Js Framework
    • Electrode JS
      • Electrode Platform
      • Electrode Question
    • Redux
      • Redux Basic Usage
      • Middleware And Asynchronous
      • React-Redux 的用法
    • NPM
      • package.json
      • semver
    • Webpack
      • 编写插件
    • 同构渲染
    • 调用DLL
  • 服务端
    • Inotify
    • Linux
    • Nginx
      • Nginx简介
      • Nginx原理、安装预配置
    • TCP/IP 协议
    • HTTP 协议
      • 基础概念篇
      • 协议详解篇
    • Process
      • 阻塞与非阻塞
      • 进程与线程优性能
  • 数据库
    • GraphQL
  • 移动端
  • 微信小程序
    • 微信小程序安装(linux)
    • 小程序第三方框架
  • 开发工具
    • 开发工具安装
    • Vim Command Collection
    • Git
      • Git Rule
      • Git Submodule
      • gitignore
    • Lerna
    • Ubuntu开发环境安装
  • 运维测试
    • Docker
      • Docker Synopsis
      • docker.sock
    • Nightwatch
    • Jest
  • 算法/数学/架构
    • 设计模式
    • 架构设计经验分享
    • 前端架构
    • 基本数据结构
    • 函数式编程
  • 软件工程
    • 软件生命周期
Powered by GitBook
On this page

Was this helpful?

  1. NodeJS
  2. NPM

semver

語義化版本(Semantic Versioning)規範的一個實現,實現了版本和版本範圍的解析、計算、比較。

semver 定義了兩種概念:

  • 版本是指例如0.4.1、1.2.7、1.2.4-beta.0 這樣表示包的特定版本的字符串。

  • 範圍則是對滿足特定規則的版本的一種表示,例如 1.2.3-2.3.4、1.x、^0.2、>1.4。

用semver 去比較版本將會是一個很好的選擇:

plugin.forEach(function () {
  if (!semver.satisfies(platformVersion, plugin.engines.platform)) {
    console.log(plugin.name, 'require', plugin.engines.platform, 'but unable to meet');
    }
  })

在你使用express 設計一個支持多版本的API服務器時,你可以這樣做:

app.get('/', appVersion('<0.6'), function (req, res) {
  res.send('less than 0.6');
  })

app.get('/', apiVersion('1.2.3 - 2.3.4'), function (req, res) {
  //..
  });

app.get('/', apiVersion('*'), function (req, res) {
  res.send('Unsupported version');
  })

選擇semver 的禮由很簡單, 讓裝專業的包去完成專業的工作

Previouspackage.jsonNextWebpack

Last updated 5 years ago

Was this helpful?