-
Notifications
You must be signed in to change notification settings - Fork 102
/
index.js
60 lines (47 loc) · 1.58 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
var express = require('express');
var app = express();
var fs = require('fs');
var engines = require('consolidate');
var JSONStream = require('JSONStream');
var bodyParser = require('body-parser');
var User = require('./db').User;
app.engine('hbs', engines.handlebars);
app.set('views', './views');
app.set('view engine', 'hbs');
app.use('/profilepics', express.static('images'));
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/favicon.ico', function (req, res) {
res.end();
});
app.get('/', function (req, res) {
User.find({}, function (err, users) {
if (err) console.error(err);
res.render('index', {users: users});
});
});
app.get('*.json', function (req, res) {
res.download('./users/' + req.path, 'virus.exe');
});
app.get('/data/:username', function (req, res) {
var username = req.params.username;
var readable = fs.createReadStream('./users/' + username + '.json');
readable.pipe(res);
});
app.get('/users/by/:gender', function (req, res) {
var gender = req.params.gender;
var readable = fs.createReadStream('users.json');
readable
.pipe(JSONStream.parse('*', function (user) {
if (user.gender === gender) return user.name;
}))
.pipe(JSONStream.stringify('[\n ', ',\n ', '\n]\n'))
.pipe(res);
});
app.get('/error/:username', function (req, res) {
res.status(404).send('No user named ' + req.params.username + ' found');
});
var userRouter = require('./username');
app.use('/:username', userRouter);
var server = app.listen(3000, function () {
console.log('Server running at http://localhost:' + server.address().port);
});