您的位置:首页 > 理论基础 > 计算机网络

【http】

2013-11-23 21:13 344 查看


var qs = require('querystring')
require('http').createServer(function(req, res) {
//res.writeHead(200, {'Content-Type': 'image/png'})
/*var stream = require('fs').createReadStream('image.png')
stream.on('data', function(data) {
res.write(data)
})
stream.on('end', function() {
res.end()
})*/
//require('fs').createReadStream('image.png').pipe(res)

if ('/' == req.url) {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end([
'<form method="POST" action="/url">',
'<input type="text" name="name">',
'<button>Submit</button>',
'</form>'
].join(''))
} else if ('/url' == req.url && 'POST' == req.method) {
var body = ''
req.on('data', function(data) {
body += data
})
req.on('end', function() {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end('Content-Type: ' + req.headers['content-type'] + 'Data:' + qs.parse(body).name)
})
} else {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end('Not Found')
}
}).listen(3000)

console.log(qs.parse('name=Guillermo'))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: