preface

B: well… No foreword today

The target

Emulate http-server to start the service and print the service information on the console

To prepare

NPM depend on the package

NPM init -y // Initialize the project NPM install http-server -g // The installation depends on NPM install commander // Develop a customized command NPM install chalk // customize the console print information colorCopy the code

Establish a connection between the local project and the local NPM module

  • File structure

  • Package. json configuration command & the file to execute
 "bin": {
    "fs": "./bin/www.js",
    "file-server": "./bin/www.js"
  },
Copy the code
  • www.js file
#! /usr/local/bin/node// specifies that the interpreter for this script is node console.log(' NPM link test OK ').Copy the code
  • npm link

The appearance of a successful link

NPM notice created a lockfile as package-lock.json. You should commit this file. NPM WARN [email protected] No Description NPM WARN [email protected] No repository field. up to date in 0.255s /usr/local/bin/fs -> /usr/local/lib/node_modules/http-server/bin/www.js /usr/local/bin/file-server -> /usr/local/lib/node_modules/http-server/bin/www.js /usr/local/lib/node_modules/http-server -> /Users/ usernames /Documents/all_demo/demos/ node-core/Network HTTP series/HTTP-serverCopy the code

Potential problem: The environment path points to the wrong path. I started with #! /user/bin/env node The NPM link succeeded, but the fs command ZSH: command not found: fs error

The /usr/bin/env command is used to find your local node installation path. The /usr/bin/env command is used to find your local node installation path.

  • User-defined command fs and file-server tests

    Both successfully printed the console in the www.js file

start

The config. Js configuration

Const option = {port: {option: "-p, --port <n>", // Based on commander option (' ') default: 8080, // Default port usage: "Fs --port 3000", // description: "set fs port", // description}}; module.exports = option;Copy the code

www.js

Commander Specifies how to use and configure the command

const program = require("commander"); const option = require("./config"); const defaultMapping = {}; Object.entries(option).forEach(([key, value]) => { defaultMapping[key] = value.default; }); Let userArgs = program.opts(); let userArgs = program. Let serverOptions = object. assign(defaultMapping, userArgs); // custom Server 🥱 const Server = require(".. /src/index"); let server = new Server(serverOptions);Copy the code

The Server class

Keeps the console for my coding process

const http = require("http"); const os = require("os"); Const chalk = require("chalk"); The os.networkInterfaces() method returns an object containing information about each network interface. The returned object will contain the network interface array let interfaces = os.networkInterfaces(); // console.log(interfaces); // interfaces are arrays where there are objects and the value of the object is an array, We need to synthesize these objects into a simple array interfaces = object.values (interfaces).reduce((memo, current) => {return memo. Concat (current); } []); Let IP = interfaces.find((item) => {// why item.family==='IPv4'&&item.cidr. StartWith ('192') // ifconfig Check my IP === en0 Return item.family === "IPv4" && item.cidr. StartsWith ("192"); }); Class Server {// 1: Constructor (serverOptions) {this.port = serverOptions. Port; } start() {// http.createserver creates a local service const server = http.createserver ((req, res) => {}); Server.listen (this.port, (err) => {console.log(Starting up http-server, serving./\n")); console.log("Available on:\n"); console.log(`http://` + ip.address + `:${chalk.green(this.port)}\n`); The console. The log (` http://127.0.0.1:${chalk. Green (enclosing port)} \ n `); }); }} module.exports = Server; // fs -- exports = Server; // fs -- exports = Server;Copy the code

test

The style is not very good

Finally, if you find this article helpful, please remember to like three times oh, thank you very much