-
Notifications
You must be signed in to change notification settings - Fork 102
/
username.js
52 lines (42 loc) · 1.17 KB
/
username.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
var express = require('express');
var helpers = require('./helpers');
var fs = require('fs');
var User = require('./db').User;
var router = express.Router({
mergeParams: true
});
router.use(function (req, res, next) {
console.log(req.method, 'for', req.params.username, 'at', req.path);
next();
});
router.get('/', function (req, res) {
var username = req.params.username;
User.findOne({username: username}, function (err, user) {
if (err) console.error(err);
res.render('user', {
user: user,
address: user.location
});
});
});
router.use(function (err, req, res, next) {
if (err) console.error(err.stack);
res.status(500).send('Something broke!');
});
router.put('/', function (req, res) {
var username = req.params.username;
User.findOne({username: username}, function (err, user) {
if (err) console.error(err);
user.name.full = req.body.name;
user.location = req.body.location;
user.save(function () {
res.end();
});
});
});
router.delete('/', function (req, res) {
var fp = helpers.getUserFilePath(req.params.username);
fs.unlinkSync(fp); // delete the file
res.sendStatus(200);
});
module.exports = router;