Created
November 25, 2013 13:01
-
-
Save colthreepv/7640874 to your computer and use it in GitHub Desktop.
Example of /user route of angular-login-example made in ExpressJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example true backend for angular-login-example | |
*/ | |
var express = require('express'); | |
var http = require('http'); | |
var path = require('path'); | |
var app = express(); | |
// from https://github.com/mrgamer/angular-login-example/blob/master/src/mockhttp.js#L51-L58 | |
var userStorage = { | |
'johnm': { name: 'John', username: 'johnm', password: 'hello', email: '[email protected]', userRole: userRoles.user, tokens: [] }, | |
'sandrab': { name: 'Sandra', username: 'sandrab', password: 'world', email: '[email protected]', userRole: userRoles.admin, tokens: [] } | |
}, | |
emailStorage = { | |
'[email protected]': 'johnm', | |
'[email protected]': 'sandrab' | |
}, | |
tokenStorage = {}; | |
// all environments | |
app.set('port', process.env.PORT || 3000); | |
app.use(express.favicon()); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
// code adapted from https://github.com/mrgamer/angular-login-example/blob/master/src/mockhttp.js#L124-L137 | |
app.get('/user', function (req, res) { | |
var queryToken, userObject; | |
// if is present in a registered users array. | |
if (queryToken = req.get('X-Token')) { | |
if (tokenStorage[queryToken] !== null ) { | |
userObject = userStorage[tokenStorage[queryToken]]; | |
return res.send(200, { token: queryToken, name: userObject.name, userRole: userObject.userRole }); | |
} else { | |
return res.send(401, 'auth token invalid or expired'); | |
} | |
} else { | |
return res.send(401, 'auth token invalid or expired'); | |
} | |
}); | |
http.createServer(app).listen(app.get('port'), function(){ | |
console.log('Express server listening on port ' + app.get('port')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "application-name", | |
"version": "0.0.1", | |
"private": true, | |
"scripts": { | |
"start": "node app.js" | |
}, | |
"dependencies": { | |
"express": "3.4.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment