首页 > web前端 > js教程 > 正文

Node.js 灵活的路由

黄舟
发布: 2017-01-17 15:55:52
原创
1382人浏览过

node.js route(路由) 

定义router.js文件

[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文件

[code]var server = require('./server');
var router = require('./router');
server.start(router.route);
登录后复制

278.png

279.png

以上就是Node.js 灵活的路由的内容,更多相关内容请关注PHP中文网(www.php.cn)!

路由优化大师
路由优化大师

路由优化大师是一款及简单的路由器设置管理软件,其主要功能是一键设置优化路由、屏广告、防蹭网、路由器全面检测及高级设置等,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号