通過所請(qǐng)求的 URL 和其他需要的 GET 及 POST 參數(shù)拒课,隨后路由需要根據(jù)這些數(shù)據(jù)來執(zhí)行相應(yīng)的代碼
- Node.JS 模塊徐勃,它們分別是 url 和 querystring 模塊。來解析
index.js
var server = require('./server');
var route = require('./router');
server.start(route);
server.js
var http = require('http');
var url = require("url");
function start(route){
function serverListener(req,res){
var pathName = url.parse(req.url).pathname;
console.log("德瑪西亞"+pathName);
res.write("hello node");
route.route(pathName)
console.log(route.route)
res.end();
}
http.createServer(serverListener).listen(8888)
console.log("SERVER IS RUN");
}
exports.start =start;
router.js
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;