http 模块 与 hello world

hello world

let http = require("http");

http
.createServer((request, response) => {
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
if (request.url !== "/favicon.ico") {
response.write("<b>hello world</>");
response.write("</br>");
response.end("<i>你好,世界</i>");
}
})
.listen(8888); console.log("server running at http://127.0.0.1:8888/");

首先引入 http 模块,然后调用 http 的 createServer 方法,创建一个服务器,最后调用 listen 监听一个端口.createServer 的第一个参数是一个函数,函数中接收 request 和 response 作为两个参数.

打开浏览器输入http://127.0.0.1:8888/就可以看到hello world

http

要使用 HTTP 服务器和客户端,必须 require('http').http 模块主要用于搭建 HTTP 服务.

http.createServer()

createServer 直接 new 一个 http.Server 的实例,传入回调函数,然后再返回新建的 http.Server 实例

listen(port, host)

http.Server 实例的方法,为 connections 启动一个 server 监听

request 对象

createServer 的文档是 http.createServer([options][, requestlistener]),request 对象是 createServer 方法中回调函数的第一个参数,自带一些属性和方法来获取客户端的请求信息和读取客户端请求的数据

  • method: 客户端请求方式
  • url: 请求的地址
  • headers: 客户端发送的请求头信息
  • httpVersion: HTTP 请求版本
  • trailers: 客户端发送的 trailers 对象信息。只有 IncommingMessage 对象的 end 事件触发后才能读取到该信息。
  • socket: 服务器端监听客户端请求的 socket 对象。
  • data 事件: 当服务器接收到客户端发送的请求数据时触发 data 事件。
  • end 事件: 当客户端发送给服务器数据执行完毕时触发 end 事件。

request 对象全部的绑定属性和方法,直接 console.log(request)

IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
paused: true,
emitClose: true,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: true,
decoder: null,
encoding: null },
readable: true,
.......
Timeout {
_called: false,
_idleTimeout: 120000,
_idlePrev: [Timeout],
_idleNext: [TimersList],
_idleStart: 108,
_onTimeout: [Function: bound ],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(unrefed)]: true,
[Symbol(asyncId)]: 9,
[Symbol(triggerId)]: 8 },
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0 },
_consuming: false,
_dumped: false }

request.url 在请求的时候会额外请求一个/favicon.ico,一般框架体系都会对这个进行处理

response 对象

response 对象由 HTTP 服务器在内部创建,表示服务器端的 HTTP 回应。 它作为第二个参数传给 'request' 事

  • writeHead: 用来写入 HTTP 回应的头信息
  • end: 写入 HTTP 回应的具体内容
  • write: 这会发送一块响应主体

http 的响应

  • setHeader(key, value):指定 HTTP 头信息。
  • write(str):指定 HTTP 回应的内容。
  • end():发送 HTTP 回应。

处理 get 请求

get 参数在 request 的 url 属性上,通过 url.parse 将 url 转化为对象

http
.createServer((request, response) => {
let pathname = url.parse(request.url).pathname;
if (pathname !== "/favicon.ico") {
if(pathname==="/login"){
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
response.write("我就是get");
response.end();
}
}
})
.listen(8888, "localhost");

处理 post 请求

当客户端采用 POST 方法发送数据时,服务器端可以对 data 和 end 两个事件,设立监听函数,data 事件会在数据接收过程中,每收到一段数据就触发一次,接收到的数据被传入回调函数。end 事件则是在所有数据接收完成后触发

    "/login": (request, response) => {
let totalData = "";
request.on("data", data => {
totalData += data;
}); request.on("end", () => {
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
response.write(totalData); //username=liudehua&password=123456&remark=%E6%88%91%E6%98%AF%E5%88%98%E5%BE%B7%E5%8D%8E%2C%E6%88%91%E6%98%AF%E4%B8%80%E5%90%8D%E6%AD%8C%E6%89%8B
response.end();
});
},

路由的简单应用

let http = require("http");

http
.createServer((request, response) => {
if (request.url !== "/favicon.ico") {
if (request.url === "/") {
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
response.end("你好,世界");
} else if (request.url === "/login") {
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
createForm(response);
response.end("登录");
} else if (request.url === "/register") {
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
createForm(response);
response.end("注册");
} else {
response.writeHead(404, { "Content-Type": "text/plain;charset=utf-8" });
response.end("404找不到相关文件");
}
}
})
.listen(8888); console.log("server running at http://127.0.0.1:8888/"); function createForm(response) {
response.write("用户名:<input type='text' name='username'>");
response.write("</br>");
response.write("密码:<input type='text' name='password'>");
response.write("</br>");
}

路由就是根据不同的选择执行不同的函数代码

url.parse 方法

解析 URL 字符串并返回 URL 对象。如果 urlString 不是字符串,则抛出 TypeError。如果 auth 属性存在但无法解码,则抛出 URIError。

语法

url.parse(urlStr, [parseQueryString], [slashesDenoteHost])

参数

  • urlStr:要解析的 URL 字符串
  • parseQueryString:如果设为 true,则返回的 URL 对象的 query 属性会是一个使用 querystring 模块的 parse() 生成的对象。 如果设为 false,则 query 会是一个未解析未解码的字符串。 默认为 false
  • slashesDenoteHost:如果设为 true,则 // 之后至下一个 / 之前的字符串会解析作为 host。 例如, //foo/bar 会解析为 {host: 'foo', pathname: '/bar'} 而不是 {pathname: '//foo/bar'}。 默认为 false。
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'localhost:8888',
port: '8888',
hostname: 'localhost',
hash: null,
search: '?username=liudehua&password=123456',
query: 'username=liudehua&password=123456',
pathname: '/login',
path: '/login?username=liudehua&password=123456',
href:
'http://localhost:8888/login?username=liudehua&password=123456' }

用处

//当路径为http://127.0.0.1:8888/register
console.log(pathname);// /register
console.log(request.url);// /register //当路径为http://127.0.0.1:8888/register?username=liudehua&password=123456
console.log(pathname);// /register
console.log(request.url);// /register?username=liudehua&password=123456

路由匹配

let http = require("http");
let url = require("url"); http
.createServer((request, response) => {
let pathname = url.parse(request.url).pathname;
if (pathname !== "/favicon.ico") {
router(pathname)(request, response);
}
})
.listen(8888, "localhost"); function router(path) {
let router = {
"/": (request, response) => {
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
response.end("你好,世界");
},
"/login": (request, response) => {
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
createForm(response);
response.end("登录");
},
"/register": (request, response) => {
response.writeHead(200, { "Content-type": "text/html;charset=utf-8" });
createForm(response);
response.end("注册");
},
"/404": (request, response) => {
response.writeHead(404, { "Content-Type": "text/plain;charset=utf-8" });
response.end("404找不到相关文件");
}
}; !Object.keys(router).includes(path) && (path = "/404"); return router[path];
} function createForm(response) {
response.write("用户名:<input type='text' name='username'>");
response.write("</br>");
response.write("密码:<input type='text' name='password'>");
response.write("</br>");
}

之后分别输入 localhost:8888,localhost:8888/haha,localhost:8888/login,localhost:8888/register

Docs

Node.js http 文档

MDN HTTP

koajs

koa-docs-Zh-CN

Http 模块

HTTP 消息头(HTTP headers)-常用的 HTTP 请求头与响应头

[Nodejs] node写个hello,world的更多相关文章

  1. 初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助

    初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助,如果有bug或者更好的优化方案,也请批评与指正,谢谢,代码如下: var fs = require('f ...

  2. nodejs,node原生服务器搭建实例

    nodejs,node原生服务器搭建实例

  3. [Nodejs] 用node写个爬虫

    寻找爬取的目标 首先我们需要一个坚定的目标,于是找个一个比较好看一些网站,将一些信息统计一下,比如 url/tag/title/number...等信息 init(1, 2); //设置页数,现在是1 ...

  4. nodejs初写心得

    nodejs安装后如何查看和安装其他工具 网上nodejs的文章已经很多,这里只是写下自己的小小心得,如果能帮到别人当然更好. 安装nodejs这里就不叙述了,直接上nodejs官网下载就好了,初学者 ...

  5. 用node写一个皖水公寓自动刷房源脚本

    因为住的地方离公司太远,每天上下班都要坐很久的班车,所以最近想搬到公司旁边的皖水公寓住.去问了一下公寓的客服,客服说房源现在没有了,只能等到别人退房,才能在网站上申请到. 如果纯靠手动F5刷新浏览器, ...

  6. [Nodejs] node实现静态文件服务器

    node 静态文件处理 一般后端进行静态文件处理都是使用 Apache nginx 等静态 web 服务器,但是既然使用 node 了,就用 node 实现以下静态服务器吧. 之前弄了不少充满艺术的数 ...

  7. [Nodejs] node的fs模块

    fs 模块 Node.js 提供一组类似 UNIX(POSIX)标准的文件操作 API. Node 导入文件系统模块(fs).Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如 ...

  8. [NodeJS] Node.js 与 V8 的故事

    要说Node.js的历史,就不得不说说V8历史.在此之前我们先一句话描述一下什么是Node.js:Node.js是一个基于Google Chrome V8 Javascript引擎之上的平台,用以创建 ...

  9. node 写api几个简单的问题

    最近出了一直在做无聊的管理后台,还抽空做了我公司的计费终端,前端vue,后端node,代码层面没啥太多的东西.由于自己node版本是8.0.0,node自身是不支持import和export的,要想基 ...

随机推荐

  1. 基于Orangpi Zero和Linux ALSA实现WIFI无线音箱(三)

    作品已经完成,先上源码: https://files.cnblogs.com/files/qzrzq1/WIFISpeaker.zip 全文包含三篇,这是第三篇,主要讲述接收端程序的原理和过程. 第一 ...

  2. pandas和spark的dataframe互转

    pandas的dataframe转spark的dataframe from pyspark.sql import SparkSession # 初始化spark会话 spark = SparkSess ...

  3. ASP.NET MVC如何做一个简单的非法登录拦截

    摘要:做网站的时候,经常碰到这种问题,一个没登录的用户,却可以通过localhost:23244/Main/Index的方式进入到网站的内部,查看网站的信息.我们知道,这是极不安全的,那么如何对这样的 ...

  4. C# 插入超链接到PDF文档(3种情况)

    超链接可以实现不同元素之间的连接,用户可以通过点击被链接的元素来激活这些链接.具有高效.快捷.准确的特点.本文中,将分享通过C#编程在PDF文档中插入超链接的方法.内容包含以下要点: 插入网页链接 插 ...

  5. 不一样的 SQL Server 日期格式化

    不一样的 SQL Server 日期格式化 Intro 最近统计一些数据,需要按天/按小时/按分钟来统计,涉及到一些日期的格式化,网上看了一些文章大部分都是使用 CONVERT 来转换的,SQL Se ...

  6. python3 生成器初识 NLP第五条

    话不多说,先把第五条抄一遍: 五,沟通的意义在于对方的回应 沟通没有对与错,只有“有效果”或者“没有效果”之分. 自己说得多“对”没有意义,对方收到你想表达的讯息才是沟通的意义. 因此自己说什么不重要 ...

  7. JVM内存结构/JVM运行时数据区,以及堆内存的划分

    1.程序计数器: 程序计数器是线程私有的内存,JVM多线程是通过线程轮流切换并分配处理器执行时间的方式实现的,当线程切换后需要恢复到正确的执 行位置(处理器)时,就是通过程序计数器来实现的.此内存区域 ...

  8. 教你如何把openfire的muc聊天室改造为群

    openfire群聊与QQ群对比 应该是去年的时候开始接触openfire,当时在分析后发现基于xmpp协议的openfire已经具备了群聊的功能.也就没太当回事,觉得加点功能就可以做成类似于QQ群的 ...

  9. Linux 桌面玩家指南:16. 使用 CUDA 发挥显卡的计算性能

    特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ...

  10. Linux常用命令速查-汇总篇

    Linux常用命令速查-用户管理 Linux常用命令速查-文件管理 Linux常用命令速查-系统监控 Linux常用命令速查-网络管理 Linux常用命令速查-定时任务 Linux常用命令速查-Vim