url.parse不是已經幫你把整個url拆解出來嗎?為何你不去Node.js API文檔看看url模組怎麽用?
var http = require('http');
var url = require('url');
http.createServer(function (req, res) {
var urlObj = url.parse(req.url, true); //拆解url
console.log('path + query = ' + urlObj.path); //讀取整個url (path + query)
console.log('path = ' + urlObj.pathname); //讀取path部份
console.log('query = ' + urlObj.search); //讀取query部份
console.log('directoryPath = ' + urlObj.query.directoryPath); //讀取某個query key的value
console.dir(urlObj, {depth: null}); //列出整個拆解後的url物件
console.dir(urlObj.query, {depth: null}); //列出整個拆解後的query物件
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello world.');
}).listen(5958);