P08:node实现静态服务器 ~ 创建项目

583 阅读5分钟

相关文章

终于开始静态静态服务器的相关学习!!!

创建一个项目


使用github进行项目搭建以及代码管理

  • 点击新建

  • 填写项目基本信息

  • 这样我们就成功在github创建了一个项目成功创建

  • 下载到本地

  • git clone XXXXXXX

介绍基本文件


  • .gitignore 是项目不必要文件不被git管理
    • 忽略文件的原则:
      • 忽略操作系统自动生成的文件,比如缩略图等;
      • 忽略编译生成的中间文件、可执行文件等,也就是如果一个文件是通过另一个文件自动生成的,那自动生成的文件就没必要放进版本库,比如Java编译产生的.class文件;
      • 忽略你自己的带有敏感信息的配置文件,比如存放口令的配置文件。
    • 常用规则:
      • 起始位# 代表注释
      • 匹配模式前 /代表项目根目录
      • 匹配模式后 /代表目录
      • 匹配模式前 !代表取反 ~ 常用与整个文件中只管理某一个或者几个文件
      • * 代表匹配任意字符
      • ** 代表匹配多级目录
      • ? 代表匹配一个字符
  • LICENSE 项目许可证协议,具体可以查看内部说明
  • README.md 项目使用手册,使用别人项目推荐先看此文件

其他配置介绍


  • npmignore 和gitignore 语法一致,但是是用来限制npm包的文件
    • 需要注意的是,在向npm上传包的时候,如果没有设置 .npmignore 就会默认使用 .gitignore
    • 有时会出现没有build包,因为其被.gitignore忽略,所以一定要注意设置好.npmignore
  • eitorconfig
    • 用来管理项目代码风格
    • 一些基本配置
    # EditorConfig is awesome: https://EditorConfig.org
    
    # top-most EditorConfig file
    root = true
    
    # Unix-style newlines with a newline ending every file
    [*]
    end_of_line = lf
    insert_final_newline = true
    
    # Matches multiple files with brace expansion notation
    # Set default charset
    [*.{js,py}]
    charset = utf-8
    
    # 4 space indentation
    [*.py]
    indent_style = space
    indent_size = 4
    
    # Tab indentation (no size specified)
    [Makefile]
    indent_style = tab
    
    # Indentation override for all JS under lib directory
    [lib/**.js]
    indent_style = space
    indent_size = 2
    
    # Matches the exact files either package.json or .travis.yml
    [{package.json,.travis.yml}]
    indent_style = space
    indent_size = 2
    

项目使用工具


  • ESlint
    • 这里不得不提eslintrc.js的文件配置,这里贴出相对详细的一份
     module.exports = {
      root: true,
      parserOptions: {
        parser: 'babel-eslint',
        sourceType: 'module'
      },
      env: {
        browser: true,
        node: true,
        es6: true,
      },
      extends: ['eslint:recommended'],
    
      // add your custom rules here
      rules: {
        'accessor-pairs': 2,
        'arrow-spacing': [2, {
          'before': true,
          'after': true
        }],
        'block-spacing': [2, 'always'],
        'brace-style': [2, '1tbs', {
          'allowSingleLine': true
        }],
        'camelcase': [0, {
          'properties': 'always'
        }],
        'comma-dangle': [2, 'never'],
        'comma-spacing': [2, {
          'before': false,
          'after': true
        }],
        'comma-style': [2, 'last'],
        'constructor-super': 2,
        'curly': [2, 'multi-line'],
        'dot-location': [2, 'property'],
        'eol-last': 2,
        'eqeqeq': [2, 'allow-null'],
        'generator-star-spacing': [2, {
          'before': true,
          'after': true
        }],
        'handle-callback-err': [2, '^(err|error)$'],
        'indent': [2, 2, {
          'SwitchCase': 1
        }],
        'jsx-quotes': [2, 'prefer-single'],
        'key-spacing': [2, {
          'beforeColon': false,
          'afterColon': true
        }],
        'keyword-spacing': [2, {
          'before': true,
          'after': true
        }],
        'new-cap': [2, {
          'newIsCap': true,
          'capIsNew': false
        }],
        'new-parens': 2,
        'no-array-constructor': 2,
        'no-caller': 2,
        'no-console': 'off',
        'no-class-assign': 2,
        'no-cond-assign': 2,
        'no-const-assign': 2,
        'no-control-regex': 2,
        'no-delete-var': 2,
        'no-dupe-args': 2,
        'no-dupe-class-members': 2,
        'no-dupe-keys': 2,
        'no-duplicate-case': 2,
        'no-empty-character-class': 2,
        'no-empty-pattern': 2,
        'no-eval': 2,
        'no-ex-assign': 2,
        'no-extend-native': 2,
        'no-extra-bind': 2,
        'no-extra-boolean-cast': 2,
        'no-extra-parens': [2, 'functions'],
        'no-fallthrough': 2,
        'no-floating-decimal': 2,
        'no-func-assign': 2,
        'no-implied-eval': 2,
        'no-inner-declarations': [2, 'functions'],
        'no-invalid-regexp': 2,
        'no-irregular-whitespace': 2,
        'no-iterator': 2,
        'no-label-var': 2,
        'no-labels': [2, {
          'allowLoop': false,
          'allowSwitch': false
        }],
        'no-lone-blocks': 2,
        'no-mixed-spaces-and-tabs': 2,
        'no-multi-spaces': 2,
        'no-multi-str': 2,
        'no-multiple-empty-lines': [2, {
          'max': 1
        }],
        'no-native-reassign': 2,
        'no-negated-in-lhs': 2,
        'no-new-object': 2,
        'no-new-require': 2,
        'no-new-symbol': 2,
        'no-new-wrappers': 2,
        'no-obj-calls': 2,
        'no-octal': 2,
        'no-octal-escape': 2,
        'no-path-concat': 2,
        'no-proto': 2,
        'no-redeclare': 2,
        'no-regex-spaces': 2,
        'no-return-assign': [2, 'except-parens'],
        'no-self-assign': 2,
        'no-self-compare': 2,
        'no-sequences': 2,
        'no-shadow-restricted-names': 2,
        'no-spaced-func': 2,
        'no-sparse-arrays': 2,
        'no-this-before-super': 2,
        'no-throw-literal': 2,
        'no-trailing-spaces': 2,
        'no-undef': 2,
        'no-undef-init': 2,
        'no-unexpected-multiline': 2,
        'no-unmodified-loop-condition': 2,
        'no-unneeded-ternary': [2, {
          'defaultAssignment': false
        }],
        'no-unreachable': 2,
        'no-unsafe-finally': 2,
        'no-unused-vars': [2, {
          'vars': 'all',
          'args': 'none'
        }],
        'no-useless-call': 2,
        'no-useless-computed-key': 2,
        'no-useless-constructor': 2,
        'no-useless-escape': 0,
        'no-whitespace-before-property': 2,
        'no-with': 2,
        'one-var': [2, {
          'initialized': 'never'
        }],
        'operator-linebreak': [2, 'after', {
          'overrides': {
            '?': 'before',
            ':': 'before'
          }
        }],
        'padded-blocks': [2, 'never'],
        'quotes': [2, 'single', {
          'avoidEscape': true,
          'allowTemplateLiterals': true
        }],
        'semi': [2, 'never'],
        'semi-spacing': [2, {
          'before': false,
          'after': true
        }],
        'space-before-blocks': [2, 'always'],
        'space-before-function-paren': [2, 'never'],
        'space-in-parens': [2, 'never'],
        'space-infix-ops': 2,
        'space-unary-ops': [2, {
          'words': true,
          'nonwords': false
        }],
        'spaced-comment': [2, 'always', {
          'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
        }],
        'template-curly-spacing': [2, 'never'],
        'use-isnan': 2,
        'valid-typeof': 2,
        'wrap-iife': [2, 'any'],
        'yield-star-spacing': [2, 'both'],
        'yoda': [2, 'never'],
        'prefer-const': 2,
        'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
        'object-curly-spacing': [2, 'always', {
          objectsInObjects: false
        }],
        'array-bracket-spacing': [2, 'never']
      }
    }
    
  • XXXignore
    • 很多工具或者依赖都会有自己独特的ignore,大同小异,不在介绍

控制项目代码质量


我们知道可以通过工具去规范我们的项目代码,让团队的代码风格趋于一致,常用的工具就是eslint。但是有一个问题,我就不遵守了,不理会报错,执意提交代码。虽说有提交审核的步骤,但是能不能在源头控制,不让不规范的代码提交到项目代码管理上(比如git)呢? 答案是:YES

  • pre-commit
    • 虽说我们可以自己去配置,来实现刚才所说的需求,但是有工具为啥不用呢
    • pre-commit 已经为我们做好了大部分配置工作
    • 只需在package.json如下配置即可实现
      "scripts": {
          "lint": "eslint --fix --ext .js,.vue src"
      },
      "pre-commit": [
          "lint"
      ],
      
    • 在提交代码前会自动执行lint ,如果出现不规范的地方,而且还不能够自动修复,就会终止commit

完事了,可以撸代码了