-
-
Save edinsoncs/572e899d88922a487ef6 to your computer and use it in GitHub Desktop.
routes
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 qs = require('querystring'); | |
var routes = { | |
'GET':{ | |
'/': function (req, res){ | |
res.writeHead(200, {'Content-type': 'text/html'}); | |
res.end('<h1>Hola que tal</h1>'); | |
}, | |
'/about': function (req, res) { | |
res.writeHead(200, {'Content-type':'text/html'}); | |
res.end('<h1>hola saludos about</h1>'); | |
}, | |
'/api/getinfo': function (req, res) { | |
res.writeHead(200, {'Content-type': 'application/json'}); | |
res.end(JSON.stringify(req.queryParams)); | |
} | |
}, | |
'POST':{ | |
'/api/login': function(req, res){ | |
var body = ''; | |
req.on('data', function data(){ | |
body += data; | |
console.log(body.length); | |
}); | |
req.on('end', function(){ | |
console.log(body); | |
console.log('usuario', params['username']); | |
console.log('password', params['password']); | |
res.end(); | |
}); | |
} | |
}, | |
'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]; | |
if(miRouter != undefined) { | |
req.queryParams = baseURI.query; | |
miRouter(req, res); | |
} | |
else { | |
routes['NA'](req, res); | |
} | |
//console.log('request del router ' + JSON.stringify(baseURI)); | |
//console.log('request del metodo ' + req.method); | |
} | |
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