nodejs 主要特点

  1. 单线程,单线程的好处,减少了内存开销,操作系统的内存换页。
  2. 非阻塞I/O, 不会傻等I/O语句结束,而会执行后面的语句。
  3. 事件机制,事件环,不管是新用户的请求,还是老用户的I/O完成,都将以事件方式加入事件环,等待调度。

简单实例:

// hello-world.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
  console.log(`服务器运行在 http://${hostname}:${port}/`);
});
// node hello-world.js
// 输出:服务器运行在 http://127.0.0.1: 3000

 主要模块及api

http模块 http.Agent 类 http.ClientRequest 类 http.Server 类 http.ServerResponse 类  
events模块 异步 VS 同步 EventEmitter       
module模块 commonjs模块化规范,一个module就是一个文件        
fs模块 fs.ReadStream类 fs.FSWatcher类  fs.WriteStream类  fs.Stats()  fs的Promise API
process模块

process.stdin

process.stdout

process.on

process.env

process.nextTick

方法将 callback 

添加到下一个时间点的队列(事件循环)

process.mainModule
querystring模块

querystring.parse

querystring.stringify      
path模块

path.join([...paths])

path.parse(path)

path.resolve([...paths])

path.join([...paths])

path.dirname(path)

debuger模块 run - 运行脚本(在调试器启动时自动运行) restart - 重启脚本 scripts - 列出所有已加载的脚本    

猜你喜欢

转载自blog.csdn.net/qq_31687021/article/details/89005581
今日推荐