-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservidor.js
More file actions
33 lines (26 loc) · 959 Bytes
/
servidor.js
File metadata and controls
33 lines (26 loc) · 959 Bytes
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
const express = require('express')
const {Contenedor} = require('./contenedor')
const app = express()
const PORT = 8080
const server = app.listen(PORT, () => {
console.log(`Servidor http escuchando en el puerto ${server.address().port}`)
})
server.on("error", error => console.log(`Error en servidor ${error}`))
app.get('/', (req, res) => {
res.send({ mensaje: 'Bienvenido al TP de Servidores con Node. por JNS' })
})
app.get('/productos', (req, res) => {
const productos = new Contenedor('./productos.txt').getAll()
res.json(productos)
})
app.get('/productos', (req, res) => {
const productos = new Contenedor('./productos.txt').getAll()
res.json(productos)
})
app.get('/productoRandom', (req, res) => {
const productos = new Contenedor('./productos.txt').getAll()
const idx = Math.floor(Math.random() * productos.length);
console.log("idx", idx)
console.log(productos[idx]);
res.json(productos[idx])
})