-
-
Save edinsoncs/410c9044f2faf64aee47 to your computer and use it in GitHub Desktop.
2 nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//router basic | |
var http = require('http'); | |
var url = require('url'); | |
var routes = { | |
'GET':{ | |
'/': function(req, res){ | |
res.writeHead(2000, {'Content-type' : 'text/html'}); | |
res.end('<h1>Hola que tal</h1>'); | |
} | |
}, | |
'POST':{ | |
}, | |
'NA': function(req,res){ | |
res.write(404); | |
res.end('Content not found!'); | |
} | |
} | |
function router(req, res){ | |
var baseURI = url.parse(req.url, true); | |
var miRouter = routes[req.method][baseURI.pathname]; | |
//console.log('request del router ' + JSON.stringify(baseURI)); | |
//console.log('request del metodo ' + req.method); | |
if(miRouter != undefined) { | |
req.queryParams = baseURI.query; | |
miRouter(req, res); | |
} else { | |
routes['NA'](req, res); | |
} | |
} | |
http.createServer(router).listen(3000, function(){ | |
console.log('Server corriendo') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment