Node学习笔记-001

1. 什么是Node

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

2. DIRT的概念

Node所针对的应用程序有一个专门的简称:DIRT。它表示数据密集型实时(data-intensive real-time)程序。 因为Node自身在I/O上非常轻量,它善于将数据从一个管道混排或代理到另一个管道上,这能在处理大量请求时持有很多开放的连接,并且只占用一小部分内存,它的设计目标是保证相应能力,更浏览器一样

3. 代码示例:Http 服务器

一个简单的http服务器的实现

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(Server running at http://${hostname}:${port}/);
});

4. 数据流

一个简单的数据流示例:

let stream = fs.createReadStream('./resource.json')

stream.on('data', (chunk) => ({ console.log(chunk) }))
stream.on('end', () = ({ console.log('finished') }))

5. 参考学习资料

  1. 进击Node.js基础(一)

  2. 进击Node.js基础(二)

  3. nodejs实战

关键字:node.js, node, const, hostname


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部