-
-
Save parzibyte/d922785d261cf3d30698f6a88132faaa to your computer and use it in GitHub Desktop.
This file contains 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
const express = require("express"), | |
path = require("path"), | |
app = express(), | |
puerto = 3000; | |
app.get('/', (peticion, respuesta) => { | |
// Podemos acceder a la petición HTTP | |
let agenteDeUsuario = peticion.header("user-agent"); | |
respuesta.send("La ruta / solicitada con: " + agenteDeUsuario); | |
}); | |
app.get('/pagina', (peticion, respuesta) => { | |
// Servir archivo HTML, o cualquier otro archivo | |
let rutaDeArchivo = path.join(__dirname, "plantilla.html"); | |
respuesta.sendFile(rutaDeArchivo); | |
}); | |
app.get('/hola', (peticion, respuesta) => { | |
let mascota = { | |
nombre: "Maggie", | |
edad: 2, | |
}; | |
respuesta.json(mascota); | |
}); | |
// Una vez definidas nuestras rutas podemos iniciar el servidor | |
app.listen(puerto, err => { | |
if (err) { | |
// Aquí manejar el error | |
console.error("Error escuchando: ", err); | |
return; | |
} | |
// Si no se detuvo arriba con el return, entonces todo va bien ;) | |
console.log(`Escuchando en el puerto :${puerto}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment