Home  >  Article  >  Web Front-end  >  Node.js flexible routing

Node.js flexible routing

黄舟
黄舟Original
2017-01-17 15:55:521169browse

Node.js route (routing)

Define the router.js file

[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;

Define the index.js file

[code]var server = require('./server');
var router = require('./router');
server.start(router.route);

Node.js flexible routing

Node.js flexible routing

The above is the content of Node.js flexible routing. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Node.js functionsNext article:Node.js functions