Related articles

  • P01: Node.js tutorial from a practical perspective
  • P02: Basic node usage
  • P03: Node built-in module PATH
  • P04: Buffer for nodeAPI
  • P05: Events of node built-in module
  • P06: Node built-in module FS (1)
  • P07: Node built-in module FS (2)
  • P08: Node implements static server ~ create project
  • P09: Node implements static server ~ Hello HTTP
  • P10: Node implements static server ~ static file or folder reading
  • P11: Node to achieve static server ~ preliminary optimization experience
  • P12: node Implements static server ~ Content-Type optimization
  • P13: Node implements static server ~ accept-encoding and Content-encoding
  • P14: Node implements the static server ~ range request
  • P15: Node implements static server ~ cache
  • P16: Node implements static server to CLI

Realize the command line function

Cli(Command line interface) is simply a command line that can quickly generate its own project templates and other functions (vue-CLI scaffolding is familiar with). In order to facilitate open source use, it is necessary to make the code written above into Cli tools.

Modify the readme. Md

Comments tell the user what functions those command line statements correspond to

Install yargs

We can parse the argument list on the command line with process.argv, but one problem with this is that it’s very difficult to parse and maintain

What are yargs? Yargs helps you build interactive command-line tools by parsing parameters and generating elegant user interfaces.

```
npm i yargs
```
Copy the code

newsrc/index.js

const yargs = require('yargs')
const Server = require('./app')

const argv = yargs
  .usage('anywhere [options]')
  .option('p', {
    alias: 'port',
    describe: 'Port number',
    default: 9527
  })
  .option('h', {
    alias: 'hostname',
    describe: 'host',
    default: '127.0.0.1'
  })
  .option('d', {
    alias: 'root',
    describe: 'root path',
    default: process.cwd()
  })
  .version()
  .alias('v'.'version')
  .help()
  .argv

const server = new Server(argv)
server.start()
Copy the code

Transform the SRC/app. Js

// Create a server instance const server = http.createserver ((rep, res) => {// Get path const filePath = path.join(conf.root, Rep. url) route(rep, res, filePath)}) server.listen(conf.port, conf.hostname, () => { const addr = `http://${conf.hostname}:${conf.port}`

  console.info(`server startd at ${chalk.green(addr)}Class Server {constructor(config) {this.conf = object.assign ({}, conf, config)})start() {const server = http.createserver ((req, res) => {// get the filePath const filePath = path.join(this.conf.root, req.url) route(req, res, filePath, this.conf) }) server.listen(this.conf.port, this.conf.hostname, () => { const addr = `http://${this.conf.hostname}:${this.conf.port}`
      console.log(`Server started at ${chalk.green(addr)}`)
    })
  }
}

module.exports = Server
Copy the code

src/header/route.js

Since the configuration source is now user-defined, we need to remove the default configuration in route.js and let the user provide it

Provided by users

Package. json adds the main option

"main": "src/app.js".Copy the code

Once written, users can either use the CLI or use the server directly. This is where the benefits of module separation come in

Preliminary experience

Js -p 8888 or node SRC /index.js --port=8888Copy the code

Automatic page opening

The new header/openUrl. Js

const { exec } = require('child_process'Module.exports = url => {switch (process.platform) {// node exports module module.exports = url => {switch (process.platform) {case 'darwin':
      exec(`open ${url}`)
      break
    case 'win32':
      exec(`start ${url}`)
      break}}Copy the code

perform

Js -p 7777 or node SRC /index.js --port=7777Copy the code

Automatic page opening

Set up the project as a CLI tool

  • Package. json adds the bin option
"bin": {// This adds the configuration to the system command line"node_anydoor": "bin/node_anydoor"
}
Copy the code
  • createbin/node_anydoor
#! /usr/bin/env node
require('.. /src/index')
Copy the code
  • Add executable permissions to files

How can open source projects elegantly use version numbers to mark project progress

^10.0.3 10.0.3 // What's the difference between these twoCopy the code
  • Describes the readable version numbers A.B.C and ^ A.B.C

    • C is the number of bugs that have been changed, and the first version is conventionally marked with 0.0.1 or 0.1.0
    • B is the project released B new features, and the new features are compatible
    • A is a major version update that may not be compatible with the previous code
    • 1.2. *~ 1.2.0Indicates to install the latest version 1.2. When downloading the dependency again, it will automatically pull the latest version under 1.2
    • 2.X^ 2.0.0As above, lock the larger version
  • Published to gitlab

    • In this case, you need to update the version number to 0.1.0
NPM publish // Global install NPM I -g your file nameCopy the code

close