142
St
startsInvasion / files
startsInvasion / node.js
3.1 KB
1/* ============================================================================ 2 author: @xonecas 3 4 A boiler inspired by apache's .htaccess, that will serve static 5 files off the shelf (just in case you want to get going quick) 6 Everything else is for you to decide. 7 8 I will keep this up to date with current node and connect versions 9 any issues please file a issue :-) 10============================================================================= */ 11 12var connect = require('connect'), 13 // inspect tool, I use it all the time. 14 inspect = require('util').inspect; 15 16 17/* ---------------------------- 18 your server code: 19---------------------------- */ 20 21 22 23 24 25 26var routes = function (app) { 27 28/* --------------------------------------------------------------------------------- 29 your routes go here 30 you can use app.get, app.post, ... 31 the docs are here: http://senchalabs.github.com/connect/middleware-router.html 32---------------------------------------------------------------------------------- */ 33 34 35 36 37 38/* --------------------------------------------------------------------------------- 39 Keep this route last. 40---------------------------------------------------------------------------------- */ 41 app.get('*', function (req, res, next) { 42 var url = req.url, 43 ua = req.headers['user-agent']; 44 45 // request latest IE engine or chrome frame 46 if (ua && ua.indexOf('MSIE') && 47 url.match(/\.html$/) || url.match(/\.htm$/)) 48 res.setHeader('X-UA-Compatible', "IE=Edge,chrome=1"); 49 50 // protect .files 51 if (url.match(/(^|\/)\./)) 52 res.end("Not allowed"); 53 54 // control cross domain using CORS (http://enable-cors.org/) 55 res.setHeader('Access-Control-Allow-Origin', '*'); 56 res.setHeader("Access-Control-Allow-Headers", "X-Requested-With"); 57 58 next(); // let the static server do the rest 59 }); 60} 61 62/* ---------------------------------------------------------------------------------- 63 set you cache maximum age, in milisecconds. 64 if you don't use cache break use a smaller value 65 start the server. 66---------------------------------------------------------------------------------- */ 67var cache = 1000 * 60 * 60 * 24 * 30, 68 port = 5551, 69 htdocs = __dirname, 70 server = connect.createServer( 71 // http://senchalabs.github.com/connect/middleware-logger.html 72 connect.logger(":date | :remote-addr | :method :url :status | :user-agent"), 73 connect.router(routes), 74 connect.static(__dirname, {maxAge: cache}) 75 ); 76 77server.listen(port); 78console.log('Node up!\nPort:\t'+port+'\nhtdocs:\t'+htdocs); 79 80/* ----------------------------------------------------------------------------------- 81 this is a failsafe, it will catch the error silently and logged it the console 82 while this works, you should really try to catch the errors with a try/catch block 83 more on this here: 84 http://nodejs.org/docs/v0.4.3/api/process.html#event_uncaughtException_ 85----------------------------------------------------------------------------------- */ 86process.on('uncaughtException', function (err) { 87 console.log('Caught exception: ' + err); 88});