-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (111 loc) · 3 KB
/
index.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var http=require('http');
var url=require('url');
var fs=require('fs');
var querystring = require('querystring');
const mime = {
'html': 'text/html',
'css': 'text/css',
'jpg': 'image/jpg',
'ico': 'image/x-icon',
'mp3': 'audio/mpeg3',
'mp4': 'video/mp4'
}
let img = "";
const servidor = http.createServer((pedido, respuesta) => {
const url = new URL('http://localhost:8888' + pedido.url)
let camino = 'public' + url.pathname
if (camino == 'public/')
camino = 'public/index.html'
encaminar(pedido, respuesta, camino)
})
servidor.listen(4444)
function encaminar(pedido, respuesta, camino) {
console.log(camino)
switch (camino) {
case 'public/recuperardatos': {
recuperar(pedido, respuesta)
break
}
default: {
fs.stat(camino, error => {
if (!error) {
fs.readFile(camino, (error, contenido) => {
if (error) {
respuesta.writeHead(500, { 'Content-Type': 'text/plain' })
respuesta.write('Error interno')
respuesta.end()
} else {
const vec = camino.split('.')
const extension = vec[vec.length - 1]
const mimearchivo = mime[extension]
respuesta.writeHead(200, { 'Content-Type': mimearchivo })
respuesta.write(contenido)
respuesta.end()
}
})
} else {
respuesta.writeHead(404, { 'Content-Type': 'text/html' })
respuesta.write('<!doctype html><html><head></head><body>Recurso inexistente</body></html>')
respuesta.end()
}
})
}
}
}
function recuperar(pedido, respuesta) {
let info = ''
pedido.on('data', datosparciales => {
info += datosparciales
})
pedido.on('end', () => {
const formulario = new URLSearchParams(info)
console.log(formulario)
respuesta.writeHead(200, { 'Content-Type': 'text/html' })
const pagina =
`<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/css/style.css">
</head>
<body>
<div class="box">
<div>
<img src="${primos(formulario.get('max'),formulario.get('min'))}" id="im" width="120" height="95" >
</div
<div>
<img src=${formulario.get('op')} id="im" width="120" height="95" >
<h1>usuario</h1>
</div>
<div>
<img src=${img} id="im" width="120" height="95" >
<h1>maquina</h1>
</div>
<div aling="center">
<a href="index.html">JUGAR DE NUEVO</a>
</div>
</div>
</div>
</body></html>`
respuesta.end(pagina)
})
}
console.log('Servidor web iniciado')
function primos(max,min){
let k=0;
let t=[];
let primos=[];
let i=0;
for(let p=min ; p<=max ; p++){
if(p%2!=0 | p==2 && p!=0){
t.push(p);
k++
}
}
console.log('los numeros impares son: ', t)
for(h=0; h<=t.length ; h++){
if(t[h]%3!=0 & t[h]%5!=0 & t[h]%7!=0){
primos.push(t[h])
}
}
console.log('los numeros primos son ', primos)
}