forked from andresvilla/puls4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·82 lines (59 loc) · 1.59 KB
/
server.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
var express = require('express'),
swig = require('swig'),
cons = require('consolidate'),
fs = require('fs'),
uuid = require('node-uuid');
var env = "dev";
var app = express(),
baseData = fs.readFileSync('./base-data.json').toString(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
var data = JSON.parse(baseData);
swig.init({
cache : false
});
// View engine
app.engine('.html', cons.swig);
app.set('view engine', 'html');
app.set('views', './app/views');
// Add POST, PUT, DELETE methods to the app
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
// Static files
app.use( express.static('./public') );
// Routes
app.get('/articles/all', function(req, res){
res.send(data);
});
app.post('/articles', function (req, res){
req.body.id = uuid.v1();
req.body.votes = 0;
req.body.image = "/img/img3.jpg";
req.body.user = "Siedrix";
data.push(req.body);
console.log('articles::create', req.body);
io.sockets.emit('articles::create', req.body);
res.send(200, {status:"Ok", id: req.body.id});
});
app.put('/articles/:id', function (req, res){
var article;
for (var i = data.length - 1; i >= 0; i--) {
article = data[i];
if(article.id === req.params.id){
data[i] = req.body;
}
}
console.log('articles::update', req.body);
io.sockets.emit('articles::update', req.body);
res.send(200, {status:"Ok"});
});
var home = function (req, res) {
res.render('index',{
posts : data,
env : env
});
};
app.get('/', home);
app.get('/article/:id', home);
server.listen(3000);