Heim >Web-Frontend >js-Tutorial >Flexibles Routing von Node.js
Node.js-Route (Routing)
router.js-Datei definieren
[code]var http = require('http'); var url = require('url'); function start(route) { var onRequest = function (request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname); route(pathname); response.writeHead(200, { "Content-Type" : "text/plain"}); response.write("Hello Zhang Shan"); response.end(); } http.createServer(onRequest).listen(8888); console.log('Server has started'); } exports.start = start;
index.js-Datei definieren
[code]var server = require('./server'); var router = require('./router'); server.start(router.route);
Das Obige ist der Inhalt des flexiblen Routings von Node.j. Weitere verwandte Inhalte finden Sie auf der chinesischen PHP-Website (www.php.cn).