nodejs繁琐地自建路由

Posted 执白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nodejs繁琐地自建路由相关的知识,希望对你有一定的参考价值。

一、繁琐的自建路由

app.js

var server = require(\'./server\');

server.startServer();

server.js

var http = require(\'http\');
var fs = require(\'fs\');

function startServer () {
    var onRequest = function(request, response) {
        if (request.url === \'/\' || request.url === \'/home\') {
            response.writeHead(200, { \'Content-Type\': \'text/html\'});
            fs.createReadStream(__dirname + \'/index.html\', \'utf8\').pipe(response);
        } else if (request.url === \'/review\') {
            response.writeHead(200, { \'Content-Type\': \'text/html\'});
            fs.createReadStream(__dirname + \'/review.html\', \'utf8\').pipe(response);
        } else if (request.url === \'/api/v1/records\') {
            response.writeHead(200, { \'Content-Type\': \'application/json\' });
            var jsonObj = {
                name: \'sfafas\',
                job: \'coder\'
            }
            response.end(JSON.stringify(jsonObj));
        } else {
            response.writeHead(200, {\'Content-Type\': \'text/html\'});
            fs.createReadStream(__dirname + \'/404.html\', \'utf8\').pipe(response);
        }
    }
    
    var server = http.createServer(onRequest);
    server.listen(3000);    
} 

console.log(\'finished\');
module.exports.startServer = startServer;

二、重构路由

app.js

var server = require(\'./server\');
var router = require(\'./router\');

var handler = require(\'./handler\');

var handle = {};
handle[\'/\'] = handler.home;
handle[\'/home\'] = handler.home;
handle[\'/review\'] = handler.review;
handle[\'/api/v1/records\'] = handler.api_records;

server.startServer(router.route,handle);

server.js

var http = require(\'http\');
var fs = require(\'fs\');

function startServer (route,handle) {
    var onRequest = function(request, response) {
        route(handle, request.url,response);
    }
    
    var server = http.createServer(onRequest);
    server.listen(3000);    
} 

console.log(\'finished\');
module.exports.startServer = startServer;

handler.js

var fs = require(\'fs\');

function home(response) {
    response.writeHead(200, { \'Content-Type\': \'text/html\'});
    fs.createReadStream(__dirname + \'/index.html\', \'utf8\').pipe(response);
}

function review(response) {
    response.writeHead(200, { \'Content-Type\': \'text/html\'});
    fs.createReadStream(__dirname + \'/review.html\', \'utf8\').pipe(response);
}

function api_records(response) {
    response.writeHead(200, { \'Content-Type\': \'application/json\' });
    var jsonObj = {
        name: \'sfafas\',
        job: \'coder\'
    }
    response.end(JSON.stringify(jsonObj));
}

module.exports = {
    home: home,
    review: review,
    api_records: api_records
}

router.js

var fs = require(\'fs\');

function route(handle, pathname,response) {
    console.log(\'Routing a request for\' + pathname);
    if (typeof handle[pathname] === \'function\') {
        handle[pathname](response);
    } else {
        response.writeHead(200, {\'Content-Type\': \'text/html\'});
        fs.createReadStream(__dirname + \'/404.html\', \'utf8\').pipe(response);
    }
}

module.exports.route = route;

三、页面整体结构

以上是关于nodejs繁琐地自建路由的主要内容,如果未能解决你的问题,请参考以下文章

nodejs常用代码片段

使用 NodeJS 和 JSDOM/jQuery 从代码片段构建 PHP 页面

javascript 用于在节点#nodejs #javascript内设置react app的代码片段

VSCode自定义代码片段11——vue路由的配置

VSCode自定义代码片段11——vue路由的配置

VSCode自定义代码片段11——vue路由的配置