NodeJs 学习使用及API 记录

本文章主要记录自己学习使用Node中遇到的问题和一些API使用的记录,不是技术贴,请大神指导!

process.argv

功能:可以获得从命令行数输入的参数:
Node文件代码:
// print process.argv
process.argv.forEach(function(val, index, array) {
console.log(index + ‘: ‘ + val);
});

运行代码:$ node process-2.js one two=three four
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four

Accessing the main module

功能:当js文件被用node命令直接运行时,require.main 就会被设定为 module 。
That means that you can determine whether a file has been run directly by testing

require.main === module

For a file foo.js, this will be true if run via node foo.js, but false if run by require(‘./foo’).

Because module provides a filename property (normally equivalent to __filename), the entry point of the current application can be obtained by checking require.main.filename.

util

工具包

util.format(format, […])

功能:格式化输出
参数占位符:
%s – 字符串.
%d – 数字 (both integer and float).
%j – JSON.
% – single percent sign (‘%’). This does not consume an argument.

If the placeholder does not have a corresponding argument, the placeholder is not replaced.

util.format(‘%s:%s’, ‘foo’); // ‘foo:%s’

If there are more arguments than placeholders, the extra arguments are converted to strings with util.inspect() and these strings are concatenated, delimited by a space.

util.format(‘%s:%s’, ‘foo’, ‘bar’, ‘baz’); // ‘foo:bar baz’

If the first argument is not a format string then util.format() returns a string that is the concatenation of all its arguments separated by spaces. Each argument is converted to a string with util.inspect().

util.format(1, 2, 3); // ‘1 2 3’

    原文作者:Karrdy
    原文地址: https://segmentfault.com/a/1190000002503157
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞