-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (67 loc) · 1.91 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
const http = require('node:http')
const fs = require('node:fs')
let text = `<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>logica-programacion-4</title>
</head>
<body>
<script>
function calculo(){
let num
let a = 0
let b = 1
let c = 0
let arr = []
let text = "La secuencia de Fibonacci de "
let element = document.createElement("p")
try{
num = parseInt(document.getElementById("fibo").value)
text = text + num + " digitos: "
for(let i = 0; i < num-1; i++){
arr.push(c)
if(c === 1) arr.push(c)
c = b + a
a = b
b = c
}
text = text + arr.join(",")
element.appendChild(document.createTextNode(text))
const respuesta = document.body.appendChild(element)
}
catch(e){
element.appendChild(document.createTextNode('Debe ingresar un número entero.'))
const respuesta = document.body.appendChild(element)
}
}
</script>
<label for="fibo">Ingrese el número para mostrar Fibonacci:</label>
<input type="text" id="fibo" name="fibo">
<button onclick="calculo()">Calcular</button>
</body>
</html>`
const createIndexHTML = () => {
fs.appendFile('./index.html', text, (err) => {
if (err) throw err
console.log('Creado')
})
}
const readIndex = (req, res) => {
fs.readFile('./index.html', (err, data) => {
if(err) {
createIndexHTML()
return readIndex(req, res)
}
res.writeHead(200, {"Content-Type": "text/html"})
res.write(data)
return res.end()
})
}
const server = http.createServer((req, res) => {
readIndex(req, res)
})
server.listen(0, () => {
console.log(`El servidor está escuchando en el puerto http://localhost:${server.address().port}`)
})