-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathserver.js
31 lines (26 loc) · 935 Bytes
/
server.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
var express = require('express'),
app = express();
app.configure(function(){
app.set('port', process.env.PORT || 5000);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.logger('dev'));
app.use(express.errorHandler());
});
app.all('/*', function(req, res, next) {
//IRL, lookup in a database or something
if (typeof req.headers['x-api-key'] !== 'undefined' && req.headers['x-api-key'] === '123myapikey') {
next();
} else {
res.status(401).send({error: "Bad or missing app identification header"});
}
});
app.get('/blog', express.basicAuth('correct', 'credentials'), function(req, res) {
res.send({posts:['one post', 'two post']});
});
app.listen(app.get('port'), function(){
console.log("Example API listening on port " + app.get('port') + ', running in ' + app.settings.env + " mode.");
});