Home > Web Front-end > JS Tutorial > Node.js flexible routing

Node.js flexible routing

黄舟
Release: 2017-01-17 15:55:52
Original
1200 people have browsed it

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;
Copy after login

Define the index.js file

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

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template