node之get與post
阿新 • • 發佈:2018-04-05
IT input 宋體 true nod lse charset pan put
Get獲取內容:
var http=require(‘http‘),
util=require(‘util‘),//util 提供常用函數集合
url=require(‘url‘); http.createServer(function(req,res){ res.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); var params=url.parse(req.url,true).query; res.write(‘www‘+params.name); res.end(); }).listen(8000)
post獲取內容
var querystring=require(‘querystring‘); var postHTML=`<html><title>post</title><body><form method=‘post‘> 名字:<input type=‘text‘ name=‘name‘ value=‘bin‘> 年齡:<input type=‘text‘ name=‘age‘ value=‘30‘> 提交:<input type=‘submit‘></form> </body></html>` http.createServer(function(req,res){ var post=‘‘; req.on(‘data‘,function(chuck){ post+=chuck }); req.on(‘end‘,function(){ res.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘}); post=querystring.parse(post); if(post.name&&post.age){ res.write(‘name:‘+post.name); res.write(‘age:‘+post.age); }else{ res.write(postHTML) } res.end() }) }).listen(3000)
node之get與post