node js(简单手工路由)

麻雀虽小 五脏俱全

var http = require("http");
var url = require("url");

var router = {}
router["/api/get"] = function(req, res){
  res.end("hello world")
}

console.log(router)

var handle = function(req ,res) {
  var x = url.parse(req.url)
  var _path = x.pathname;
  
  router[_path]?router[_path](req,res):res.end("404");
}

var server = http.createServer(handle)

server.listen("9998")

你可能感兴趣的:(node js(简单手工路由))