[关闭]
@yangfch3 2016-05-20T13:46:10.000000Z 字数 2225 阅读 2385

临时文件

临时


链接:7-days-NodeJS
链接:7-days-NodeJS 笔记(上)

网络操作

  1. NodeJS 被创造出来的动机就是为了编写高性能 Web 服务器

  2. NodeJS 能帮助我们了解网络编程,学习 HTTP 协议和 Socket 协议

  3. 在 Linux 系统下,监听 1024 以下端口需要 root 权限

网络相关 API

与网络编程相关的 API 主要包含在以下库里:

HTTP 模块

链接:Node.js Docs_HTTP

  1. 'http' 模块提供两种使用方式:

    • 作为服务端使用时,创建一个HTTP服务器,监听HTTP客户端请求并返回响应。
    • 作为客户端使用时,发起一个HTTP客户端请求,获取服务端响应。
  2. .createServer 方法创建一个服务器,.listen 方法监听端口,Node.js 在 http 服务器中仍旧采用事件驱动执行的机制

  3. 空行之上是 请求头,之下是 请求体。HTTP 请求在发送给服务器时,可以认为是按照从头到尾的顺序一个字节一个字节地 以数据流方式发送

  4. http 模块创建的 HTTP 服务器在接收到完整的请求头后,就会 调用回调函数。在回调函数中,除了可以使用 request 对象访问请求头数据外,还能把 request 对象当作一个只读数据流来访问请求体数据

    1. http.createServer(function (request, response) {
    2. var body = [];
    3. console.log(request.method);
    4. console.log(request.headers);
    5. request.on('data', function (chunk) {
    6. body.push(chunk);
    7. });
    8. request.on('end', function () {
    9. body = Buffer.concat(body);
    10. console.log(body.toString());
    11. });
    12. }).listen(80);
    13. ------------------------------------
    14. POST
    15. { 'user-agent': 'curl/7.26.0',
    16. host: 'localhost',
    17. accept: '*/*',
    18. 'content-length': '11',
    19. 'content-type': 'application/x-www-form-urlencoded' }
    20. Hello World

    HTTP_module_demo.png-24.2kB

  5. HTTP 响应本质上也是一个数据流,同样由响应头(headers)和响应体(body)组成,在回调函数中,除了可以 使用 response 对象来写入响应头数据 外,还能 response 对象当作一个只写数据流来写入响应体数据

    1. http.createServer(function (request, response) {
    2. response.writeHead(200, { 'Content-Type': 'text/plain' });
    3. request.on('data', function (chunk) {
    4. response.write(chunk);
    5. });
    6. request.on('end', function () {
    7. response.end();
    8. });
    9. }).listen(80);
  6. http 模块在客户端模式下可以用于发起 HTTP 请求的作用:使用 http.request()http.get()(便捷版 API)等。需要指定目标服务器的位置并发送请求头和请求体,这些数据以对象的形式传入方法中,并支持回调

    1. var http = require('http');
    2. var options = {
    3. hostname: 'http://localhost',
    4. port: 8848,
    5. path: '/',
    6. method: 'POST',
    7. headers: {
    8. 'Content-Type': 'application/x-www-form-urlencoded'
    9. }
    10. };
    11. var request = http.request(options, function (response) {});
    12. // 便捷版 get 请求
    13. // http.get('http://www.example.com/', function (response) {});
    14. request.write('Hello World');
    15. request.end();
  7. 当客户端发送请求并 接收到完整的服务端响应头 时,就会调用回调函数。在回调函数中,除了可以使用 response 对象 访问响应头数据 外,还能 response 对象当作一个只读数据流来访问响应体数据

    1. var http = require('http');
    2. http.get('http://www.baidu.com/', function (response) {
    3. var body = [];
    4. console.log(response.statusCode);
    5. console.log(response.headers);
    6. response.on('data', function (chunk) {
    7. body.push(chunk);
    8. });
    9. response.on('end', function () {
    10. body = Buffer.concat(body);
    11. console.log(body.toString());
    12. });
    13. });
    14. ---响应---
    15. 200
    16. {
    17. ...
    18. }
    19. <!DOCTYPE html>
    20. ...

HTTPS

  1. https 模块与 http 模块十分相似,区别在于 https 模块需要额外处理 SSL 证书

  2. 2.

进程管理

异步编程

一个完整的示例


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注