美文网首页
global object

global object

作者: a89cb37a0ee2 | 来源:发表于2017-07-02 15:51 被阅读0次

process 是node应用与用户环境的桥梁,process.env则为用户环境的拷贝。我们可以通过process设置port以及各种环境配置,但在生产中不建议这样做。我们应该设置一个配置文件专门用于环境的各种配置,然后再const config = require('xxxx/config')进来进行配置。

process是EventEmiter的一个实例。所以你可以emit事件以及监听事件。

process.on(exit, code => {

//  在process停止之前,这里可以做最后的sync操作

console.log(`exit with ${code}`)

})

当event loop无事可做或者当你手动执行process.exit的时候exit事件会触发

process.on('uncaughtException', err => {

//  js exception is not handle

// do any cleanup and exit anyway

console.error(err) // do not do just that
//  FORCE exit the process too

process.exit(1)

})

//  keep the event loop busy

proces.stdin.resume()

//  trigger a typeError exception

console.dog()

BUFFER

```
const { StringDecoder } = require('string_decoder')

const decoder = new StringDecoder('utf8')

process.stdin.on('readable', () => {

const chunk = process.stdin.read()

if (chunk != null) {

  const buffer = Buffer.from([chunk])

console.log('with toString():', buffer.toString())

// if you are receiving bytes as chunk in a stream. you should always use String Decoder

console.log('with StringDecoder:', decoder.write(buffer) )

}

})
```

相关文章

  • global object

    process 是node应用与用户环境的桥梁,process.env则为用户环境的拷贝。我们可以通过proces...

  • Node入门教程(5)第四章:global 全局变量

    global - 全局变量 全局对象(global object),不要和 全局的对象( global objec...

  • Javascript之Array对象

    The JavaScriptArrayobject is a global object thatis used ...

  • JavaScript 全局对象

    全局的对象( global objects )或称标准内置对象,不要和 "全局对象(global object)"...

  • JS递归,预编译

    变量提升经典题目:第一题: 利用预编译的Global Object和Activation Object思想来分析:...

  • Node.js<五>

    Node.js 全局对象 JavaScript 中有一个特殊的对象,称为全局对象(Global Object),它...

  • node.js(十四)

    Node.js 全局对象JavaScript 中有一个特殊的对象,称为全局对象(Global Object),它及...

  • Node.js中的全局对象与全局变量(1)

    全局对象 JavaScript 中有一个特殊的对象,称为全局对象(Global Object),它及其所有属性都可...

  • Algorithms demo(javascript)

    暗杀教室 1、反向输出一个字符串 参考链接 Global String Object String.prototy...

  • node核心模块

    1、全局对象 JavaScript中有一个特殊的对象,称为全局对象(Global Object),它及其所有属性都...

网友评论

      本文标题:global object

      本文链接:https://www.haomeiwen.com/subject/dbubqxtx.html