Created
December 9, 2019 15:31
-
-
Save dwaps/0a289291968937e4fd2673315eedbf2c to your computer and use it in GitHub Desktop.
NODE: Exercice routing simple 1
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
<!DOCTYPE html> | |
<html lang="fr"> | |
<head> | |
<meta charset="utf-8"> | |
<title>TITLE</title> | |
<link rel="stylesheet" href="css/style.css"> | |
</head> | |
<body> | |
<h1>Bienvenue sur mon site !</h1> | |
<script src="js/app.js"></script> | |
</body> | |
</html> |
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 http = require('http'); | |
const { readFileSync } = require('fs'); | |
http.createServer( | |
function (req, res) { | |
const filePath = (req.url === '/') | |
? 'index.html' | |
: req.url.replace('/', ''); | |
if (filePath.includes('ico')) { | |
res.end(); | |
return; | |
} | |
if (filePath.includes('html')) { | |
res.setHeader('Content-Type', 'text/html; charset=utf-8'); | |
} | |
else if (filePath.includes('css')) { | |
res.setHeader('Content-Type', 'text/css'); | |
} | |
else if (filePath.includes('js')) { | |
res.setHeader('Content-Type', 'text/javascript'); | |
} | |
res.end(readFileSync(filePath)); | |
} | |
).listen(6767); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment