I. Console Color Printing
Download console-color-mr
npm install console-color-mr --save-dev
The first usage:
Direct introduction of require('console-color-mr');
Modified the default color. console.info will output red directly
//use color console.info('------------ default color--------'); console.info('info', 'fff'); //green text console.warn('this is warn');//yellowBG text console.error('this is error');//red text console.debug('this is debug');//gray text console.log('this is log','msg1'.red, 'msg2'.blue); console.info('this is info','msg1'.red, 'msg2'.blue); //force change default color console.info('----------------------------');
Use discoloration in variables or functions
console.group('---------variable use color------------'); let name = 'Michael'; let age = 1000; let obj = { name : 'michael', age : '100' }; function hello() { return 'hello'; } function isBoole() { return true; } console.log(name); console.log('Hello,My name is ' + name.green + ',I am a' + ' man'.yellow + '.'); console.log(age.blue); console.log(obj.name.blue); console.log(obj.name.greenBG); console.log(hello().red); //Boolean value must change to string. console.log(isBoole().toString().red); console.groupEnd();
The second usage:
After the first method is introduced, the original system object method will be modified. If you still want to use methods on system objects, save this reference with a variable.
let _console = require('console-color-mr');
_console.info('info'); _console.debug('debug'); _console.warn('warn'); _console.error('error');
style
'bold' 'italic' 'underline' 'inverse' 'strikethrough' 'white' 'grey' 'black' 'blue' 'cyan' 'green' 'magenta' 'red' 'yellow' 'whiteBG' 'greyBG' 'blackBG' 'blueBG' 'cyanBG' 'greenBG' 'magentaBG' 'redBG' 'yellowBG'
Reference link: https://blog.csdn.net/michael51/article/details/79035459
2. Implementing port and ip printing by koa (similar to vue)
1. Download os first
npm install os --save-dev
2. introduction
const os = require('os');
3. use
function getIPv4() { //The same interface may have more than one IP4v address, so use array storage let ipv4s = []; //Getting list objects of network interfaces let interfaces = os.networkInterfaces(); Object.keys(interfaces).forEach(function(key) { interfaces[key].forEach(function(item) { //Skip IPv6 and'127.0.0.1' if ('IPv4' !== item.family || item.internal !== false) return; ipv4s.push(item.address); //Available ipv4s join arrays // console.log(key + '--' + item.address); }) }) return ipv4s[0]; //Return an available one. } let ipv4 = getIPv4();//LAN IP
Example:
const Koa = require('koa'); const app = new Koa(); // Ways of direct invocation // const router = require('koa-router')(); // Or create router instances separately const Router = require('koa-router'); const router = new Router(); const os = require('os'); const port = 3000; require('console-color-mr'); //Get the local ipv4 address function getIPv4() { //The same interface may have more than one IP4v address, so use array storage let ipv4s = []; //Getting list objects of network interfaces let interfaces = os.networkInterfaces(); Object.keys(interfaces).forEach(function(key) { interfaces[key].forEach(function(item) { //Skip IPv6 and'127.0.0.1' if ('IPv4' !== item.family || item.internal !== false) return; ipv4s.push(item.address); //Available ipv4s join arrays // console.log(key + '--' + item.address); }) }) return ipv4s[0]; //Return one available } let ipv4 = getIPv4(); app.use(async (ctx, next) => { // ctx.body = 'Hello World'; await next(); }); router.get('/home', async ctx => { ctx.body = 'Hello Router'; }) // Startup routing app.use(router.routes()).use(router.allowedMethods()) // Above is the official recommendation. allowedMethods is used after routes to set response header based on ctx.status. app.listen(port, () => { console.log('Listening at ' + 'http://localhost:'.green + port.green + '\n'.green + 'or at ' + 'http://'.green + ipv4.green + ':'.green + port.green) });