Browse Source

refactor

pull/4/head
Peter Johnson 10 years ago
parent
commit
972d3a2a84
  1. 49
      index.js
  2. 10
      middleware/cors.js
  3. 12
      middleware/headers.js
  4. 24
      middleware/jsonp.js

49
index.js

@ -1,49 +1,13 @@
var pkg = require('./package'),
app = require('express')();
var app = require('express')();
/** ----------------------- middleware ----------------------- **/
// generic headers
app.use(function(req, res, next){
res.header('Charset','utf8');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.header('Access-Control-Allow-Credentials', true);
res.header('Server', 'Pelias/'+pkg.version);
res.header('X-Powered-By', 'mapzen');
next();
});
// jsonp middleware
// override json() to handle jsonp
app.use(function(req, res, next){
res._json = res.json;
res.json = function( data ){
// jsonp
if( req.query && req.query.callback ){
res.header('Content-type','application/javascript');
return res.send( req.query.callback + '('+ JSON.stringify( data ) + ');' );
}
// regular json
res.header('Content-type','application/json');
return res._json( data );
};
next();
});
// enable client-side caching of 60s by default
app.use(function(req, res, next){
res.header('Cache-Control','public,max-age=60');
next();
});
/** ----------------------- Routes ----------------------- **/
app.use( require('./middleware/headers') );
app.use( require('./middleware/cors') );
app.use( require('./middleware/jsonp') );
/** ----------------------- routes ----------------------- **/
// api root
app.get( '/', require('./controller/index') );
@ -53,7 +17,6 @@ app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/sugge
/** ----------------------- error middleware ----------------------- **/
// handle application errors
app.use( require('./middleware/404') );
app.use( require('./middleware/500') );

10
middleware/cors.js

@ -0,0 +1,10 @@
function middleware(req, res, next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.header('Access-Control-Allow-Credentials', true);
next();
}
module.exports = middleware;

12
middleware/headers.js

@ -0,0 +1,12 @@
var pkg = require('../package');
function middleware(req, res, next){
res.header('Charset','utf8');
res.header('Cache-Control','public,max-age=60');
res.header('Server', 'Pelias/'+pkg.version);
res.header('X-Powered-By', 'mapzen');
next();
}
module.exports = middleware;

24
middleware/jsonp.js

@ -0,0 +1,24 @@
function middleware(req, res, next){
// store old json function
var json = res.json.bind(res);
// replace with jsonp aware function
res.json = function( data ){
// jsonp
if( req.query && req.query.callback ){
res.header('Content-type','application/javascript');
return res.send( req.query.callback + '('+ JSON.stringify( data ) + ');' );
}
// regular json
res.header('Content-type','application/json');
return json( data );
};
next();
}
module.exports = middleware;
Loading…
Cancel
Save