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) )

}

})
```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 6,414评论 0 6
  • 个人入门学习用笔记、不过多作为参考依据。如有错误欢迎斧正 目录 简书好像不支持锚点、复制搜索(反正也是写给我自己看...
    kirito_song阅读 2,505评论 1 37
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    w_zhuan阅读 3,642评论 2 41
  • //公共引用 varfs =require('fs'), path =require('path'); 1、读取文...
    才気莮孒阅读 841评论 0 1
  • 模块化公布自己的模块功能. 其他模块调用另外的模块. 公布方法 如果要输出一个键值对象{},可以利用exports...
    httIsHere阅读 535评论 0 0