Commit d3ec5b8a authored by Home's avatar Home
Browse files

TP10 seems good

parent b8064f65
//const express= require('express');
const swig = require('swig');
const session = require('express-session')
const parser =require('body-parser');
const users = require('./user');
const messages = require('./messages');
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(session({
secret: 'keyboard'
}));
app.use(parser.urlencoded({extended: true}));
app.use(function(req,res,next)
{
console.log('method -> ' + req.method);
console.log('url -> ' + req.url);
next();
});
let connected={};
io.sockets.on('connection', function (socket) {
let user;
socket.on('init',function(c){
user=c.user;
connected[c.user]={socket,page:c.page};
});
socket.on('disconnect', function(){
delete connected[user];
})
});
app.get('/signin',function(req,res){
if(req.session.user)
{
res.redirect('/');
}
res.render('signin', {title:'Signin',msg:req.session.msg})
});
app.post('/signin',function(req,res){
if(users.exists(req.body.login))
{
if(users.checkPassword(req.body.login,req.body.password))
{
req.session.user=req.body.login;
res.redirect('/');
}
else {
req.session.msg= 'User ou mdp inconnue';
res.redirect('/signin');
}
}
else {
req.session.msg= 'User ou mdp inconnue';
res.redirect('/signin');
}
});
app.get('/', function(req,res)
{
if(!req.session.user)
{
return res.redirect('/signin');
}
res.render('home', { title: 'Home',user:req.session.user});
});
app.get('/signout',function(req,res){
delete req.session.user;
res.redirect('/signin');
});
app.get('/message/public',function(req,res)
{
if(!req.session.user)
{
return res.redirect('/signin');
}
res.render('messagepub',{
title: 'Message publics',
user:req.session.user,
messages:messages.getPublic()
});
});
app.post('/message/public',function(req,res){
if(!req.session.user)
{
return res.redirect('/');
}
messages.ajoutMessagePub(req.session.user,req.body.text);
for (user in connected) {
if (connected[user].page === 'public' && user !==req.session.user) {
connected[user].socket.emit('newMsg',{ author : req.session.user,
text : req.body.text });
}
}
res.redirect('/message/public')
})
app.get('/message/private',function(req,res)
{
if(!req.session.user)
{
return res.redirect('/');
}
res.render('messagepriv',{
title: 'Messages privés',
user:req.session.user,
messages:messages.getPrivate(req.session.user),
msg:req.session.msg
})
});
app.post('/message/private',function(req,res){
if(!req.session.user)
{
return res.redirect('/');
}
if(req.session.user===req.body.dest)
{
req.session.msg='Impossible de s\'envoyer des messages';
}
else {
messages.ajoutMessagePriv(req.session.user,req.body.dest,req.body.text);
req.session.msg='Ajout réussi';
for (user in connected) {
if (connected[user].page === 'private' && user===req.body.dest) {
connected[user].socket.emit('newMsg',{ author : req.session.user,
text : req.body.text });
}
}
}
res.redirect('/message/private');
});
app.use(function(req,res,next){
res.header('Content-type','text/html');
res.status(404).render('404',{ title : '404',user : req.session.user});
})
server.listen(8080);
/*
*/
var messages =[{text:'1er Message',dest:'',author:'Samuel'},
{text:'2eme Message',dest:'Samuel',author:'Jean'}];
/*function getUser(name){
return users.find(function(u){
return u.username ===name;
}):
}*/
/*function exists(name)
{
return getUser(name)!=undefined;
}*/
function getPublic()
{
return messages.filter(function(m){
return m.dest==='';
})
}
function getPrivate(name)
{
return messages.filter(function(m){
return m.dest===name;
})
}
function ajoutMessagePub(author,text)
{
messages.push({text,dest:'',author});
}
function ajoutMessagePriv(author,dest,text)
{
messages.push({text,dest,author});
}
module.exports={
ajoutMessagePub,
ajoutMessagePriv,
getPublic,
getPrivate
}
var users=[
{username: 'jokeeffe',pw: 'jokeeffe'},{username : 'toto',pw: 'titi'}
];
function getUser(name){
return users.find(function(u){
return u.username ===name;
})
}
function exists(name)
{
return getUser(name)!=undefined;
}
function checkPassword(name,pw)
{
let u =getUser(name);
return u&&u.pw===pw;
}
module.exports={
exists,
checkPassword
}
{%extends 'layout.html'%}
{% block content %}
<p>404 not found</p>
{% endblock %}
{%extends 'layout.html'%}
{% block content %}
<script src="/socket.io/socket.io.js"></script>
<h1>TP 10</h1>
{% endblock %}
<!DOCTYPE html> <html lang="en">
<head>
<meta charset="utf-8">
<title>{{ title }} </title>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
{% if user %}
<li><a href="/message/public">Messages public</a></li>
<li><a href="/message/private">Messages privee</a></li>
<li><a href="/signout">Signout</a></li>
{% else %}
<li><a href="/signin">Signin</a></li>
{% endif %}
</ul>
</nav>
{% block content %} {% endblock %}
</body>
</html>
<{%extends 'layout.html'%}
{% block content %}
<h1>Messages Privés</h1>
{%if msg%}
<strong>{{msg}}</strong>
{% endif %}
<p>Bienvenues</p>
<div id="messages">
{% for message in messages %}
<article>
<p> Auteur : {{ message.author }} </p>
<p> Message : {{ message.text }} </p>
</article>
{% endfor %}
</div>
<h3>Ajouter un message</h3>
<form action="" method="post">
<p><label for="dest">Veuillez entrer le pseudo du destinaire :</label>
<input type="text" name="dest" placeholder="Veuillez entrer votre nom"></p>
<p><label for="text">Veuillez entrer votre message :</label>
<input type="textarea" name="text" placeholder="Veuillez entrer votre message"></p>
<input type="submit" value="valider">
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
let user = '{{ user}}';
socket.on('connect',function(){
socket.emit('init',{page:'private', user});
});
socket.on('newMsg', function(message) {
var div = document.getElementById("messages");
var article = document.createElement("article");
var p1 = document.createElement("P");
var p2 = document.createElement("P");
p1.innerHTML="Autheur : " + message.author;
p2.innerHTML="Message : " + message.text;
article.appendChild(p1);
article.appendChild(p2);
div.appendChild(article);
});
</script>
{% endblock %}
{%extends 'layout.html'%}
{% block content %}
<h1>Messages Publiques</h1>
<div id="messages">
{% for message in messages %}
<article>
<p> Auteur : {{ message.author }} </p>
<p> Message : {{ message.text }} </p>
</article>
{% endfor %}
</div>
<h3>Ajouter un message</h3>
<form action="" method="post">
<p><label for="text">Veuillez entrer votre message :</label>
<input type="textarea" name="text" placeholder="Veuillez entrer votre message"></p>
<input type="submit" value="valider">
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
let user = '{{ user}}';
socket.on('connect',function(){
socket.emit('init',{page:'public', user});
});
socket.on('newMsg', function(message) {
var div = document.getElementById("messages");
var article = document.createElement("article");
var p1 = document.createElement("P");
var p2 = document.createElement("P");
p1.innerHTML="Autheur : " + message.author;
p2.innerHTML="Message : " + message.text;
article.appendChild(p1);
article.appendChild(p2);
div.appendChild(article);
});
</script>
{% endblock %}
{%extends 'layout.html'%}
{% block content %}
<h1>{{ title }}</h1>
{%if msg%}
<strong>{{msg}}</strong>
{% endif %}
<p>Bienvenue sur mon site</p>
<form action="" method="post">
<p><label for="login">Veuillez entrer votre login</label>
<input type="text" name="login" placeholder="Login" value="toto"></p>
<p><label for="password">Veuillez entrer votre mot de passe</label>
<input type="password" name="password" placeholder="Mot de passe" value="titi"></p>
<input type="submit" value="valider">
</form>
{% endblock %}
{
"name": "week1",
"name": "node",
"version": "1.0.0",
"description": "",
"main": "index.js",
......@@ -9,9 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"express-session": "^1.14.2",
"swig": "^1.4.2"
"socket.io": "^1.7.1"
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment