app.get('/',function(req,res) {var url = req.query.url var name = req.query.name console.log(url, name)})
二 接收Get
1. get参数在req.url上
2. 使用url.parse将数据由字符串转变为obj
index.js:
var http = require('http')var url = require('url')var util = require('util')//req 请求信息 res返回信息http.createServer(function(req, res){res.writeHeader(200, {'Content-Type':'text/javascriptcharset=UTF-8'}) //状态码+响应头属性 // 解析 url 参数var params = url.parse(req.url, true).query //parse将字符串转成对象,req.url="/?url=123&name=321",true表示params是{url:"123",name:"321"},false表示params是url=123&name=321res.write("网站名:" + params.name) res.write("\n") res.write("网站 URL:" + params.url) res.end()}).listen(3000)
浏览器打开:
http://127.0.0.1:3000/?url=123&name=321
网页显示:
网站名:321网站 URL:123
供你参考:client.js
var http = require('http')
http.get("http://127.0.0.1:3000/wechat?signature=d34f1983fb3ecf933b9435ad5c05b6e3a138a0e5&echostr=5898562696761927225×tamp=137", function(res) {
console.log("Got response: " + res.statusCode)
}).on('error', function(e) {
console.log("Got error: " + e.message)
})
server.js
var http = require('http')
var url = require('url')
var qs = require('querystring')
http.createServer(function(req, res) {
res.writeHead(200, {"Content-Type": "text/plain"})
var query = url.parse(req.url).query
res.end(JSON.stringify(qs.parse(query)))
}).listen(3000)