如何像NodeJS的$_GET“查询”命令一样在$_GET中使用$_GET查询字符串?
http://php.net/manual/en/reserved.variables.get.php
例如,您希望对下面的代码进行扩展,以便在?option=1部件上使用/page001。
所以访问ServerURL/page001?选项=1会写"Page1: Option001 Selected“。
和ServerURL/page001?选项=2将写入"Page1: Option002 Selected“等。
var http = require('http');
var fs = require("fs");
var location = require('location-href');
var HttpDispatcher = require('httpdispatcher');
var dispatcher = new HttpDispatcher();
////////////////////
var http=require('http');
var url=require('url');
var server=http.createServer(function(req,res){
var pathname=url.parse(req.url).pathname;
switch(pathname){
case '/page001':
res.end('<div id="page001"><h2>Page 001</h2><div>Page 001 Content</div></div>');
console.log("Page 001 Loaded");
break;
case '/page002':
res.end('<div id="page002"><h2>Page 002</h2><div>Page 002 Content</div></div>');
console.log("Page 002 Loaded");
break;
default:
res.end('<div id="default"><h2>Default</h2><div>This is the default page</div></div>');
console.log("Default page was loaded");
break;
}
}).listen(8080);
console.log("Running on Port 8080");发布于 2017-03-30 12:17:55
您必须解析所解析的URL的.search属性。关于如何在这个站点上解析查询字符串,有很多示例。
var reqUrl = url.parse(req.url),
search = reqUrl.search,
pathname = reqUrl.pathname;https://stackoverflow.com/questions/43117602
复制相似问题