一、控制臺彩色打印
下載console-color-mr
npm install console-color-mr --save-dev
第一種用法:
直接引入 require('console-color-mr');
修改了默認(rèn)顏色。console.info會直接輸出紅色
//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('----------------------------');
在變量中或者函數(shù)中使用變色
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();
第二種用法:
第一種引入之后,會對原有的系統(tǒng)對象方法做修改。如果您仍然想使用系統(tǒng)對象上的方法的請用一個變量保存這個引用。
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’
參考鏈接:https://blog.csdn.net/michael51/article/details/79035459
二、koa實現(xiàn)端口和ip打印(類似于vue)
1.先下載os
npm install os --save-dev
2.引入
const os = require('os');
3.使用
function getIPv4() {
//同一接口可能有不止一個IP4v地址,所以用數(shù)組存
let ipv4s = [];
//獲取網(wǎng)絡(luò)接口列表對象
let interfaces = os.networkInterfaces();
Object.keys(interfaces).forEach(function(key) {
interfaces[key].forEach(function(item) {
//跳過IPv6 和 '127.0.0.1'
if ('IPv4' !== item.family || item.internal !== false) return;
ipv4s.push(item.address); //可用的ipv4s加入數(shù)組
// console.log(key + '--' + item.address);
})
})
return ipv4s[0]; //返回一個可用的即可
}
let ipv4 = getIPv4();//局域網(wǎng)IP
示例:
const Koa = require('koa');
const app = new Koa();
// 直接調(diào)用的方式
// const router = require('koa-router')();
// 或 單獨(dú)創(chuàng)建router的實例
const Router = require('koa-router');
const router = new Router();
const os = require('os');
const port = 3000;
require('console-color-mr');
//獲取本機(jī)ipv4地址
function getIPv4() {
//同一接口可能有不止一個IP4v地址,所以用數(shù)組存
let ipv4s = [];
//獲取網(wǎng)絡(luò)接口列表對象
let interfaces = os.networkInterfaces();
Object.keys(interfaces).forEach(function(key) {
interfaces[key].forEach(function(item) {
//跳過IPv6 和 '127.0.0.1'
if ('IPv4' !== item.family || item.internal !== false) return;
ipv4s.push(item.address); //可用的ipv4s加入數(shù)組
// console.log(key + '--' + item.address);
})
})
return ipv4s[0]; //返回一個可用的即可
}
let ipv4 = getIPv4();
app.use(async (ctx, next) => {
// ctx.body = 'Hello World';
await next();
});
router.get('/home', async ctx => {
ctx.body = 'Hello Router';
})
// 啟動路由
app.use(router.routes()).use(router.allowedMethods())
// 以上為官方推薦方式,allowedMethods用在routes之后,作用是根據(jù)ctx.status設(shè)置response header.
app.listen(port, () => {
console.log('Listening at ' + 'http://localhost:'.green + port.green + '\n'.green + 'or at ' + 'http://'.green +
ipv4.green + ':'.green + port.green)
});
