-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
96 lines (79 loc) · 2.46 KB
/
app.js
File metadata and controls
96 lines (79 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const express = require( "express" );
const fs = require( "fs" ).promises;
const app = express();
app.set( 'views', 'views' );
app.set( 'view engine', 'ejs' );
app.use( express.static('static'));
app.use( express.urlencoded({extended:true}) );
app.use( express.json() );
// accede a la pagina de inicio
app.get( '/', (req, res, next)=>{
res.render( 'index' );
res.end();
});
//accede a la pag del registro cuando pinchas el link
app.get( '/regist', (req, res, next)=>{
res.render( 'regist' );
res.end();
});
//compruebas q estes registrado o no
app.get( '/pag', async(req, res, next)=>{
try {
let user, pasw, array;
user = req.query.user;
pasw = req.query.pasw;
array = await read_document();
for( let i = 1; i <= array[0].total_users; i++ ){
if( user == array[i].user ){
if( pasw == array[i].pasw ){
res.send( "<h1 style='color:green; text-align: center; margin-top: 80px; font-size: 80px;'> Working on it...</h1>" )
}
}
//console.log( array[i].user + ' '+ array[i].pasw );
}
res.send( "<h1 style='color:red; text-align: center; margin-top: 80px; font-size: 60px;'> no estas registrado papasito</h1>" )
}
catch(err){
}
});
//te registra en la pagina
app.post( '/regist', async (req, res, next)=>{
try{
let user, pasw, array, index, result;
user = req.body.user;
pasw = req.body.pasw;
array = await read_document();
array[0].total_users += 1;
index = array[0].total_users;
array[index] = { user: user, pasw: pasw };
result = JSON.stringify( array );
await write_document( result );
console.log( "ok" );
res.render( 'index' );
}
catch(err){
console.log( err );
}
});
app.listen(1313);
//------------------------------------------------------------------------
// esta es la funcion q lee el txt y lo almacena en la var array
const read_document = async ()=>{
try{
const document = await fs.readFile( './users.txt' );
return JSON.parse(document);
}
catch(err){
console.log( err );
return err;
}
}
//esta es la funcion q escribe( guarda los datos ) en el txt
const write_document = async ( result )=>{
try{
await fs.writeFile( './users.txt', result );
}
catch(err){
console.log( err );
}
}