nodejs控制臺打印彩色及使用koa實現(xiàn)端口和ip打印

一、控制臺彩色打印

下載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)
});

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天翻開筆記本,看看我去年寫的生日愿望有沒有實現(xiàn)? 我寫一些對朋友的期許,都實現(xiàn)了,但是我自己的期許還有一項沒實現(xiàn)...
    砥礪_b109閱讀 221評論 0 0
  • 書籍是我們了解歷史和認(rèn)識世界的一扇窗口。書籍是經(jīng)驗教訓(xùn)的結(jié)晶,是人類寶貴的精神財富,是采掘不盡的礦藏,是走向未來的...
    1葉兒閱讀 730評論 2 1

友情鏈接更多精彩內(nèi)容