码迷,mamicode.com
首页 > Web开发 > 详细

nodejs繁琐地自建路由

时间:2019-01-05 13:30:11      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:cti   exp   name   html   http   text   for   dir   分享   

一、繁琐的自建路由

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繁琐地自建路由

标签:cti   exp   name   html   http   text   for   dir   分享   

原文地址:https://www.cnblogs.com/bbcfive/p/10223889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!