global object

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)