summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoHong Li2017-04-12 05:55:36 +0800
committerRaccoon Li2017-05-08 19:24:38 +0800
commite2ac73f5a36310dead9e23f46004ccef909f317b (patch)
tree66250a7882c7a0ec7e37e79df309fb272ac2c158
parente3fde01e3ad2ab8c33aae0ec2960117fd223d242 (diff)
refactor(app.js): Extract /me page
-rw-r--r--app.js29
-rw-r--r--lib/web/userRouter.js36
2 files changed, 37 insertions, 28 deletions
diff --git a/app.js b/app.js
index 6f707056..2f06de94 100644
--- a/app.js
+++ b/app.js
@@ -199,34 +199,7 @@ app.use(require('./lib/web/baseRouter'))
app.use(require('./lib/web/statusRouter'))
app.use(require('./lib/web/auth'))
app.use(require('./lib/web/historyRouter'))
-
-// get me info
-app.get('/me', function (req, res) {
- if (req.isAuthenticated()) {
- models.User.findOne({
- where: {
- id: req.user.id
- }
- }).then(function (user) {
- if (!user) { return response.errorNotFound(res) }
- var profile = models.User.getProfile(user)
- res.send({
- status: 'ok',
- id: req.user.id,
- name: profile.name,
- photo: profile.photo
- })
- }).catch(function (err) {
- logger.error('read me failed: ' + err)
- return response.errorInternalError(res)
- })
- } else {
- res.send({
- status: 'forbidden'
- })
- }
-})
-
+app.use(require('./lib/web/userRouter'))
// upload image
app.post('/uploadimage', function (req, res) {
var form = new formidable.IncomingForm()
diff --git a/lib/web/userRouter.js b/lib/web/userRouter.js
new file mode 100644
index 00000000..ecfbaf8b
--- /dev/null
+++ b/lib/web/userRouter.js
@@ -0,0 +1,36 @@
+'use strict'
+
+const Router = require('express').Router
+
+const response = require('../response')
+const models = require('../models')
+const logger = require('../logger')
+
+const UserRouter = module.exports = Router()
+
+// get me info
+UserRouter.get('/me', function (req, res) {
+ if (req.isAuthenticated()) {
+ models.User.findOne({
+ where: {
+ id: req.user.id
+ }
+ }).then(function (user) {
+ if (!user) { return response.errorNotFound(res) }
+ var profile = models.User.getProfile(user)
+ res.send({
+ status: 'ok',
+ id: req.user.id,
+ name: profile.name,
+ photo: profile.photo
+ })
+ }).catch(function (err) {
+ logger.error('read me failed: ' + err)
+ return response.errorInternalError(res)
+ })
+ } else {
+ res.send({
+ status: 'forbidden'
+ })
+ }
+})