forked from francogarcino/express-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (88 loc) · 3.06 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
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const logger = require('morgan');
const cors = require('cors');
formatDate = (timestamp) => {
const dateObj = new Date(timestamp);
const day = (dateObj.getDate()).toString().padStart(2, '0');
const month = (dateObj.getMonth() + 1).toString().padStart(2, '0');
const year = dateObj.getFullYear();
const hours = dateObj.getHours().toString().padStart(2, '0');
const minutes = dateObj.getMinutes().toString().padStart(2, '0');
return "["+day+"/"+month+"/"+year+" at "+hours+":"+minutes+"]"
}
require('dotenv').config();
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(cors())
mongoose.connect(process.env.MONGODB_URI)
.then(() => {
console.log('Conectado a MongoDB Atlas');
})
.catch((error) => {
console.error(error);
});
const router = express.Router()
const Spirit = require('./models/current.model');
const http = require("http");
router.get('/', function(req, res) {
res.status(200).json("Connected with e[P]ers")
});
router.get('/espiritus', async (req, res) => {
try {
const response = await Spirit.find({})
const espiritus = response.map(doc => {
return { ...doc.toObject(), found_at: formatDate(doc.found_at) }
})
res.status(200).json(espiritus)
} catch (error) {
res.status(500).json({ "fallo_astral" : error.message })
}
})
router.get('/:name', async (req, res) => {
try {
const name = req.params.name
const espiritu = await Spirit.findOne({
name: name
})
if (!espiritu) {
return res.status(404).json({ "fallo_astral": "El espiritu " + name + " no se invocó"});
}
res.status(200).json(
{ ...espiritu.toObject(), found_at: formatDate(espiritu.found_at) }
)
} catch (error) {
res.status(500).json({ "fallo_astral": error.message });
}
})
router.post('/descubrir/:name', async (req, res) => {
const name = req.params.name
try {
const trimmed = name.trim()
if (trimmed.length > 20 || trimmed.length < 1) {
return res.status(400).json({
"fallo_astral" : "Un espiritu puede llamarse con un maximo de 20 caracteres"
})
}
const exists = await Spirit.exists({ name: trimmed })
if (exists) {
return res.status(400).json({
"fallo_astral" : "Ya existe el espiritu " + trimmed + " en la base"
})
}
const spirit = Spirit({ name: trimmed })
await spirit.save()
res.status(201).json({ ...spirit.toObject(), found_at: formatDate(spirit.found_at) })
} catch (error) {
res.status(500).json({ message: error.message });
}
})
app.use('/', router);
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
const port = process.env.PORT || '3000';
app.set('port', port);
const server = http.createServer(app);
server.listen(port);