handlers.js 具体的处理接口
function start(response){
response.writeHead(200, {
"Content-Type": "text/plain"});
response.write("start");
response.end();
}
function end(response){
response.writeHead(200, {
"Content-Type": "text/plain"});
response.write("end");
response.end();
}
function other(response){
response.writeHead(200, {
"Content-Type": "text/plain"});
response.write("other");
response.end();
}
exports.start=start;
exports.end=end;
exports.other=other;
router.js 路由处理
function route(response,url,handlers){
let handler = handlers[url];
if (typeof handler != 'function'){
handler = handlers["other"];
}
handler(response);
}
exports.route = route;
server.js 服务器创建
const http = require("http");
function start(router,handlers){
function create(request,response) {
const url = request.url;
const urlStr = url.substring(1);
router.route(response,urlStr,handlers);
}
http.createServer(create).listen(8888);
console.log("服务器启动....")
}
exports.start = start;
index.js 入口
const server = require("./server")
const router = require("./route")
const handlers = require("./handlers")
server.start(router,handlers);
资源下载:https://download.csdn.net/download/dmw412724/24308269