You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.5 KiB
74 lines
2.5 KiB
/*
|
|
Faire un site en node, tourant sur le port 80.
|
|
Le site permet de saisir un email et un mot de passe dans un formulaire.
|
|
En retour de soumission du formulaire le site affichera l'email, la chaine MD5 du mot de passe, et l'heure+date de la tentative connexion.
|
|
*/
|
|
|
|
/*
|
|
Votre réponse sera sous forme d'un zip.
|
|
Elle contiendra au minimum :
|
|
- un fichier README indiquant comment installer votre projet et les routes disponibles
|
|
- un fichier package.json
|
|
- un fichier front.js contenant votre code Node
|
|
*/
|
|
import http from 'http';
|
|
import * as url from 'url';
|
|
|
|
// npm i md5
|
|
// https://www.npmjs.com/package/md5
|
|
import md5 from 'md5';
|
|
|
|
function outputHTML5(status, response, content, title)
|
|
{
|
|
response.writeHead(status, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
response.write(
|
|
`
|
|
<!DOCTYPE html>
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>${title}</title>
|
|
<style type="text/css">*{ font-family: sans-serif }</style>
|
|
</head>
|
|
<body>
|
|
${content}
|
|
</body>
|
|
</html>
|
|
`
|
|
);
|
|
response.end();
|
|
}
|
|
|
|
var server = http.createServer(function (request, response) {
|
|
if(request.url === "/" && request.method==="GET") {
|
|
let formHtml = "<form method='post' action='/'>";
|
|
formHtml += "<label for='email'>Email</label><input id='email' type='email' value='' name='email' />";
|
|
formHtml += "<br/><label for='password'>Mot de passe</label><input id='password' type='password' value='' name='password' />";
|
|
formHtml += "<br/><button type='submit'>aller !</button>"
|
|
formHtml += "</form>";
|
|
outputHTML5(200,response,formHtml,"login");
|
|
}
|
|
else if(request.url === "/" && request.method==="POST") {
|
|
let responseHTML ="informations saisies : ";
|
|
let responseString = "?";
|
|
request.on('data', function (varposted) {
|
|
responseHTML += varposted;
|
|
responseString += varposted;
|
|
}).on('end', function () {
|
|
let parsedData = url.parse(responseString, true);
|
|
console.log("ParsedData", parsedData);
|
|
Object.keys(parsedData.query).forEach(function(key) {
|
|
var value=parsedData.query[key];
|
|
if(key==='password') {
|
|
value=md5(value);
|
|
}
|
|
responseHTML+= "<br/>"+key+" : "+value;
|
|
});
|
|
outputHTML5(200,response,responseHTML,"login");
|
|
});
|
|
} else {
|
|
outputHTML5(404,response,'Y\'a rien ici',"Oups");
|
|
}
|
|
});
|
|
|
|
server.listen(80); |