refactor(app.js): Extract /me page

This commit is contained in:
BoHong Li 2017-04-12 05:55:36 +08:00 committed by Raccoon Li
parent e3fde01e3a
commit e2ac73f5a3
2 changed files with 37 additions and 28 deletions

29
app.js
View file

@ -199,34 +199,7 @@ app.use(require('./lib/web/baseRouter'))
app.use(require('./lib/web/statusRouter')) app.use(require('./lib/web/statusRouter'))
app.use(require('./lib/web/auth')) app.use(require('./lib/web/auth'))
app.use(require('./lib/web/historyRouter')) app.use(require('./lib/web/historyRouter'))
app.use(require('./lib/web/userRouter'))
// 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'
})
}
})
// upload image // upload image
app.post('/uploadimage', function (req, res) { app.post('/uploadimage', function (req, res) {
var form = new formidable.IncomingForm() var form = new formidable.IncomingForm()

36
lib/web/userRouter.js Normal file
View file

@ -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'
})
}
})