73
Ch
Chatroom / files
Chatroom / server.js
4.4 KB
1// author: Sean Caetano Martin (xonecas) 2// 3// Rob Righter https://github.com/robrighter/node-boilerplate 4// made a node boilerplate, but I find that it does too much 5// I like making my own routes, and organize them as I see fit 6// this applies for server errors and 404's 7// So I made this, a more compact boiler, that will serve static 8// files off the shelf (just in case you want to get going quick) 9// Everything else is for you to decide 10// 11// I will keep this up to date with current node and connect versions 12// any issues please file a issue 13// 14 15// add some mime-types 16// mime already defines some for us, soon they'll support all 17var mime = require('mime'); 18 19// define early so that connect sees them 20mime.define({ 21 'application/x-font-woff': ['woff'], 22 'image/vnd.microsoft.icon': ['ico'], 23 'image/webp': ['webp'], 24 'text/cache-manifest': ['manifest'], 25 'text/x-component': ['htc'], 26 'application/x-chrome-extension': ['crx'] 27}); 28 29// make sure you install Connect (npm install connect) 30// find the docs here: http://senchalabs.github.com/connect/ 31var connect = require('connect'), 32 io = require('socket.io'), 33 // inspect tool, I use it all the time. 34 inspect = require('util').inspect; 35 // uncomment this if you plan on concatenate files 36 // var fs = require('fs'); 37 38// concatenate files, ahead of server start for better performance 39// for high concurrency servers this step's callback must init the 40// server or the files being requested might not be ready. 41// read and merge jquery and js/script.js 42/* 43fs.readFile(__dirname+'/js/libs/jquery-1.5.js', 'utf8', function (err, jquery) { 44 fs.readFile(__dirname+'/js/script.js', 'utf8', function (err, script.js) { 45 fs.writeFile(__dirname+'/js/bundle.js', jquery + script, 'utf8', function (err) { 46 if (err) throw err; 47 // file is written 48 }); 49 }); 50}); 51*/ 52 53var routes = function (app) { 54 // your routes go here 55 // you can use app.get, app.post, ... 56 // the docs are here: http://senchalabs.github.com/connect/middleware-router.html 57 58 59 60 61 62 // this must be the last route, its an addition to the static provider 63 app.get('*', function (req, res, next) { 64 var reqPath = req.url; // connect populates this 65 66 // use this header for html files, or add it as a meta tag 67 // to save header bytes serve it only to IE 68 // user agent is not always there 69 var userAgent = req.headers['user-agent']; 70 if (userAgent && userAgent.indexOf('MSIE') && 71 reqPath.match(/\.html$/) || reqPath.match(/\.htm$/)) 72 res.setHeader('X-UA-Compatible', "IE=Edge,chrome=1"); 73 74 // protect .files 75 if (reqPath.match(/(^|\/)\./)) 76 res.end("Not allowed"); 77 78 next(); // let the static server do the rest 79 }); 80} 81 82// set you cache maximum age, in milisecconds. 83// if you don't use cache break use a smaller value 84var oneMonth = 1000 * 60 * 60 * 24 * 30; 85 86// start the server 87var server = connect.createServer( 88 // good ol'apache like logging 89 // you can customize how the log looks: 90 // http://senchalabs.github.com/connect/middleware-logger.html 91 connect.logger(), 92 93 // call to trigger routes 94 connect.router(routes), 95 96 // set to ./ to find the boilerplate files 97 // change if you have an htdocs dir or similar 98 // maxAge is set to one month 99 connect.static(__dirname+'/htdocs', {maxAge: oneMonth}) 100); 101 102// bind the server to a port, choose your port: 103server.listen(1111); // 80 is the default web port and 443 for TLS 104 105// socket.io 106var socket = io.listen(server); 107socket.on('connection', function(client){ 108 // new client is here! 109 client.on('message', function(data) { 110 data = JSON.parse(data); 111 console.log(inspect(data)); 112 113 if (data.joined) { 114 data.author = 'system'; 115 data.color = '#999'; 116 data.text = data.joined +" has joined"; 117 } 118 else if (data.leaving) { 119 data.author = 'system'; 120 data.text = data.leaving +" has left"; 121 } 122 123 socket.broadcast(data); 124 }) 125 //client.on('disconnect', function(){ … }) 126}); 127 128// Your server is running :-) 129console.log('Node server is running!'); 130 131// this is a failsafe, it will catch the error silently and logged it the console 132// while this works, you should really try to catch the errors with a try/catch block 133// more on this here: http://nodejs.org/docs/v0.4.3/api/process.html#event_uncaughtException_ 134process.on('uncaughtException', function (err) { 135 console.log('Caught exception: ' + err); 136});