Created
February 7, 2020 00:25
-
-
Save fransafu/2e282ebd81d6c09962befb4a9b4c7e99 to your computer and use it in GitHub Desktop.
[Node.js] Servidor HTTP simple con rutas
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
// Llamar Biblioteca nativa para servidor HTTP | |
const http = require('http'); | |
// Crear el servidor HTTP | |
const server = http.createServer((req, res) => { | |
// Observa como vienen las rutas | |
// console.log(req.url); | |
if (req.url === '/') { | |
res.write('Ahora te encuentras en el Home "/"'); | |
return res.end(); | |
} | |
if (req.url === '/users') { | |
res.write('Ahora te encuentras en la ruta de los usuarios'); | |
return res.end(); | |
} | |
res.write('Mensaje generico cuando la ruta no se encuentra especificada'); | |
return res.end(); | |
}); | |
// Especificar el puerto del servidor | |
server.listen(8080, (err) => { | |
console.log('Server listen on port 8080'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment