You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

10 years ago
10 years ago
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('X-Powered-By', 'pelias');
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 ----------------------- **/
10 years ago
// api root
app.get( '/', require('./controller/index') );
// suggest API
app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest') );
10 years ago
10 years ago
/** ----------------------- error middleware ----------------------- **/
// handle application errors
app.use( require('./middleware/404') );
app.use( require('./middleware/500') );
10 years ago
app.listen( process.env.PORT || 3100 );