这是一个简单的HTTP服务器,我们可以填写表单并提交,页面跳转后将数据通过chunk输出到新的页面中
const http=require('http')
,fs=require('fs')
,qs=require('querystring')
var server=http.createServer(function(req,res){
if('/'==req.url){
res.writeHead(200,{'Content-type':'text/html'})
res.end(
['<form method="POST" action="/url">'
,'<h1>myform</h1>'
,'<fieldset>'
,'<label>Personal information</label>'
,'<p>What is your name?</p>'
,'<input type="text" name="name">'
,'<button>submit</button>'
,'</form>'].join(''))
}else if('/url'==req.url&&req.method=="POST"){
var body=''
req.on('data',function(chunk){
body+=chunk
})
req.on('end',function(){
res.writeHead(200,{'Content-type':'text/html'})
res.end(`<p>your name is <b>${qs.parse(body).name}</b></p>`)
})
}else{
res.writeHead(404)
res.end('404 not found')
}
})
server.listen(3000,function(){
console.log('server is on ')
})
1、创建服务器
var server=http.createServer(callback)
callback:function(req,res)
2、对request进行监听
我们在第一次进入页面时,进入的url是空,因此对是否为空进行判断,如果不为空,则给response头写状态码,并让response发送“404 not found”。
当我们进入页面并填写表单时,进入的url为"/url",因此判断是否等于该值,如果等于则给response头写状态码,并让response发送输入的值
3、发送输入的数据
监听当进入的/url的时,监控request的输入,存入body,设置发送头和类型。但当进入url时我们发现request的头类型是encoded的,也就是说我们需要通过queryString来解析数据并获得key=name时的值。
一、一个简单的HTTP服务器和客户端
1.GET请求
服务端代码
var http=require("http")
var server=http.createServer(function(req,res){
if('/'==req.url){
console.log('request success,sending data')
res.writeHead(200,{'Content-Type':'text/html'})
res.end('Hello world')
}else{
console.log('request fail')
}
})
server.listen(port=3000,function(){
console.log(`the server is listening on${port}`)
})
客户端代码
var http=require("http")
var body=''
var options=
{host:"127.0.0.1",
port:"3000",
url:'/',
method:'GET'}
var client=http.request(options,function(res){
res.setEncoding('utf8')
console.log("sending request")
res.on('data',function(chunk){
body+=chunk
console.log(body)
})
res.on('end',function(res){
console.log("request over")
})
})
client.end()
遇到的一个小问题,当服务端没有设置res.end的时候,客户端无法监听到res.on('end')的时间,说明了服务端要结束请求时,得先写end才能让客户端监听到,否则客户端会一直保持和服务端的连接。也需要在服务端一侧手动触发end时间,使request结束。
2.POST请求
服务端代码
var http=require("http")
var qs=require('querystring')
var server=http.createServer(function(req,res){
var body=''
if('/'==req.url){
console.log('request success,sending data')
req.on('data',function(chunk){
body+=chunk
})
req.on('end',function(){
console.log(`got name: ${qs.parse(body).name}`)
})
res.writeHead(200,{'Content-Type':'text/html'})
res.end('Done')
}else{
console.log('request fail')
}
})
server.listen(port=3000,function(){
console.log(`the server is listening on${port}`)
})
客户端代码 客户端通过process的流处理输入name,对name的对象进行封装,转换成JSON格式的数据(qs.stringrify)再进行发送
var http=require("http")
var qs=require("querystring")
var body=''
var options={host:"127.0.0.1",
port:"3000",
url:'/',
method:'POST'}
function send(name){
http.request(options,function(res){
res.setEncoding('utf8')
console.log("sending request")
res.on('data',function(chunk){
body+=chunk
console.log(body)
})
res.on('end',function(res){
console.log("request done")
//process.stdout.write('\n your name : ')
})
console.log(qs.stringify({name:name}))
}).end(qs.stringify({name:name}))
}
process.stdout.write('\n your name : ')
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data',function(name){
send(name.replace('\r\n',''))
})