美文网首页
兄弟会8.9号笔记

兄弟会8.9号笔记

作者: 创可贴231 | 来源:发表于2016-08-10 08:58 被阅读0次

node.js模块学习

http 模块

fs     模块

url    模块

http 模块

HTTP

http.STATUS_CODES

http.createServer([requestListener])

http.createClient([port], [host])

Class: http.Server

事件 : 'request'

事件: 'connection'

事件: 'close'

Event: 'checkContinue'

事件: 'connect'

Event: 'upgrade'

Event: 'clientError'

server.listen(port, [hostname], [backlog], [callback])

server.listen(path, [callback])

server.listen(handle, [callback])

server.close([callback])

server.maxHeadersCount

server.setTimeout(msecs, callback)

server.timeout

Class: http.ServerResponse

事件: 'close'

response.writeContinue()

response.writeHead(statusCode, [reasonPhrase], [headers])

response.setTimeout(msecs, callback)

response.statusCode

response.setHeader(name, value)

response.headersSent

response.sendDate

response.getHeader(name)

response.removeHeader(name)

response.write(chunk, [encoding])

response.addTrailers(headers)

response.end([data], [encoding])

http.request(options, callback)

http.get(options, callback)

Class: http.Agent

new Agent([options])

agent.maxSockets

agent.maxFreeSockets

agent.sockets

agent.freeSockets

agent.requests

agent.destroy()

agent.getName(options)

http.globalAgent

Class: http.ClientRequest

Event 'response'

Event: 'socket'

事件: 'connect'

Event: 'upgrade'

Event: 'continue'

request.write(chunk, [encoding])

request.end([data], [encoding])

request.abort()

request.setTimeout(timeout, [callback])

request.setNoDelay([noDelay])

request.setSocketKeepAlive([enable], [initialDelay])

http.IncomingMessage

事件: 'close'

message.httpVersion

message.headers

message.rawHeaders

message.trailers

message.rawTrailers

message.setTimeout(msecs, callback)

message.method

message.url

message.statusCode

message.socket

简单实例

var http = require('http') ;

var server = http.createServer(function(req,res){

res.writeHeader(200,{

'Content-Type' : 'text/plain;charset=utf-8'  // 添加charset=utf-8

}) ;

res.end("Hello,干嘛!") ;

}) ;

server.listen(8888) ;

console.log("http server running on port 8888 ...") ;

运行结果

Hello,干嘛!

fs 模块

文件系统模块是一个简单包装的标准 POSIX 文件 I/O 操作方法集。可以通过调用 require("fs") 来获取该模块。文件系统模块中的所有方法均有异步和同步版本。

(1),文件系统模块中的异步方法需要一个完成时的回调函数作为最后一个传入形参。

(2),回调函数的构成由调用的异步方法所决定,通常情况下回调函数的第一个形参为返回的错误信息。

(3),如果异步操作执行正确并返回,该错误形参则为null或者undefined。如果使用的是同步版本的操作方法,一旦出现错误,会以通常的抛出错误的形式返回错误。

(4),可以用try和catch等语句来拦截错误并使程序继续进行。

我们先看一个简单的例子,读取文件("bb.txt"):

(1),建立文件"bb.txt“,如下内容(”大家好“)。

(2),读取文件操作如下代码:

事例

var fs = require("fs") ;

fs.readFile("bb.txt","utf8",function (error,data){

if(error) throw error ;

console.log(data) ;

}) ;

运行结果  :

大家好

url    模块

处理HTTP请求时url模块使用率超高,因为该模块允许解析URL、生成URL,以及拼接URL。首先我们来看看一个完整的URL的各组成部分。

1 var url = require('url');

2 var queryUrl = "http://localhost:8888/bb?name=bigbear&memo=helloworld" ;

3 console.log(typeof url.parse(queryUrl)) ;

4 console.log(url.parse(queryUrl)) ;

1 object // typeof

2

3 {

4     protocol: 'http:',

5     slashes: true,

6     auth: null,

7     host: 'localhost:8888',

8     port: '8888',

9     hostname: 'localhost',

10     hash: null,

11     search: '?name=bigbear&memo=helloworld',

12     query: 'name=bigbear&memo=helloworld',

13     pathname: '/bb',

14     path: '/bb?name=bigbear&memo=helloworld',

15     href: 'http://localhost:8888/bb?name=bigbear&memo=helloworld'

16 }

加以说明如下:

protocol: 请求协议

host: URL主机名已全部转换成小写, 包括端口信息

auth:URL中身份验证信息部分

hostname:主机的主机名部分, 已转换成小写

port: 主机的端口号部分

pathname: URL的路径部分,位于主机名之后请求查询之前

search: URL 的“查询字符串”部分,包括开头的问号。

path: pathname 和 search 连在一起。

query: 查询字符串中的参数部分(问号后面部分字符串),或者使用 querystring.parse() 解析后返回的对象。

hash: URL 的 “#” 后面部分(包括 # 符号)

补充api:"url.format(urlObj)"

作用:输入一个 URL 对象,返回格式化后的 URL 字符串。

相关文章

  • 8.12兄弟会笔记

    1. MongoDB 2.express MongoDB MongoDB 是一个基于分布式文件存储的数据库。由 C...

  • 8.10兄弟会笔记

    node.js 交互 QueryString模块 QueryString" 模块用于实现URL参数字符串与参数对象...

  • 8.11兄弟会笔记

    服务器集群 简单说,分布式是以缩短单个任务的执行时间来提升效率的,而集群则是通过提高单位时间内执行的任务数来提升效...

  • 20160906兄弟会笔记

    LTS版 生产环境越越稳定越好,不是越新越好 全球最大的开源项目就是linux gnu协议 Linux命令:...

  • 兄弟会8.8号笔记

    1.linux基本命令 2.node.js linux基本操作命令 ls 显示文件或目录 -l 列出...

  • 兄弟会8.9号笔记

    node.js模块学习 http 模块 fs 模块 url 模块 http 模块 HTTP http.STA...

  • 第一天 全栈工程师

    兄弟会-全栈工程师 兄弟会-全栈工程师 第一天笔记 markdown文档编写 全栈工程师: 网站服务器搭建+数据设...

  • 兄弟会第四天笔记

    兄弟会第四天笔记 linux 基本命令【ubuntu】 ls -l 列如:drwxr-x--- 2 root ...

  • 02

    兄弟会第二天笔记 —–Ubuntu系统 Ubuntu系统简介 1、Ubuntu 16.04 LTS:LTS即长期支...

  • 兄弟会精英班 - 学习笔记(五)

    Nodejs的基本语法 Js基础知识学习参考 W3cSchool Vim创建test.js的nodejs文件va...

网友评论

      本文标题:兄弟会8.9号笔记

      本文链接:https://www.haomeiwen.com/subject/mnodsttx.html