Skip to content

Instantly share code, notes, and snippets.

@dwaps
Created December 9, 2019 15:31
Show Gist options
  • Save dwaps/0a289291968937e4fd2673315eedbf2c to your computer and use it in GitHub Desktop.
Save dwaps/0a289291968937e4fd2673315eedbf2c to your computer and use it in GitHub Desktop.
NODE: Exercice routing simple 1
<!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>
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