-
Notifications
You must be signed in to change notification settings - Fork 0
/
testserver.js
158 lines (85 loc) · 2.98 KB
/
testserver.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var http = require('http');
var express = require('express');
var manejadorDeEventos = {};
var manejadorDeBase = {};
var mu = require('mu2')
var request = require('request');
var _ = require('underscore');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var mongoose = require('mongoose');
var baseMongo = null;
mu.root = __dirname + '/templates';
var noticias = [];
var titleHackaton = "Hackaton";
var noticia = {};
noticia.tituloNoticia = "Un fuerte terremoto sacudió Japón";
noticia.resumenNoticia = "Alcanzó los 6,6 grados de magnitud en la escala abierta de Ritcher. El temblor se sintió en la prefectura de Tottori, en el oeste nipón, sin que se active la alerta de tsunami.";
noticia.link = "unlinkbienpiolah.com";
noticia.pathImagen = "../images/pic02.jpg";
noticias.push(noticia)
mongoose.connect('mongodb://localhost/admin')
var url = 'mongodb://localhost:27017/admin';
var app = express();
var id = 0;
var posts = {};
var visitas = 0;
manejadorDeEventos.getUltimo = function(res){
res.end(JSON.stringify(posts[posts.length-1]));
};
manejadorDeEventos.getIndex = function(res){
//var idReq = req.params.id;
mu.clearCache();
var stream = mu.compileAndRender('index.html', {'noticias': noticias, 'titleHackaton': titleHackaton});
stream.pipe(res);
};
var errorPath = "error404.html";
app.get('/', function(req,res){
manejadorDeEventos.getIndex(res);
});
app.get('/index.html', function (req, res) {
manejadorDeEventos.getIndex(res);
});
app.get('/index', function (req, res) {
manejadorDeEventos.getIndex(res);
});
app.delete('/posts/:id', function (req, res) {
res.end(JSON.stringify(posts[id]));
});
app.get('/catastrofe.html',function(req, res){
res.sendFile(__dirname + "/templates/catastrofe.html");
})
app.get('/addCatastrofe.html', function(req, res){
res.sendFile(__dirname + "/templates/catastrofe.html");
})
app.get('/cargar_catastrofe', function (req, res) {
nuevoDocumento = {
resumen:req.query.noticia,
titulo:req.query.titulo,
link:req.query.link,
imagen:req.query.imagen
};
console.log(baseMongo);
baseMongo.insertOne(nuevoDocumento);
res.sendFile( __dirname + "/templates/index.html" );
})
app.get('/posts', function (req, res) {
res.end(JSON.stringify(posts));
})
app.use('/assets/css', express.static(__dirname + '/assets/css'));
app.use('/images', express.static(__dirname + '/images'));
app.use('/assets/js', express.static(__dirname + '/assets/js'));
var server = app.listen(process.env.PORT || 3000, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
server.listen(process.env.PORT || 3000);
console.log('Connection established to', url);
baseMongo = db.collection('catastrofes');
}
});