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