Skip to content

Instantly share code, notes, and snippets.

@geta6
Created October 29, 2012 07:50
Show Gist options
  • Save geta6/3972198 to your computer and use it in GitHub Desktop.
Save geta6/3972198 to your computer and use it in GitHub Desktop.
Simple Server Sample
<h1>About</h1>
<p>This is simple server.</p>
<h1>Contact</h1>
<p>Your Contact Info</p>
<h1>Profile</h1>
<p>Your Profile</p>
<html>
<head>
<style>
html {
font: 12px/2.0 HelveticaNeue,Helvetica,Arial,Sans-Serif;
color: #3B4854;
}
a {
color: #4CC2E4;
text-decoration: none;
}
a:hover {
color: #4CD6FC;
}
h1 {
font-size: 1.5em;
font-weight: bold;
}
p {
margin-bottom: 1em;
}
.exterior {
position: relative;
margin: 20px 0;
float: left;
}
.interior {
position: relative;
background: white;
}
.fallback {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-image: url(/img/load.gif);
background-repeat: no-repeat;
}
#header .exterior {
float: left;
width: 150px;
}
#header .interior {
text-align: center;
}
#leader .exterior {
margin-left: 40px;
}
#leader .interior {
min-height: 240px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
(function(time){
$(document).on('click', 'a', function (event) {
var href = $(event.target).attr('href');
if (href.match(/#/)) {
return true;
} else if (href.match(/^https*:\/\//) || href.match(/^\/\//)) {
window.open(href);
} else {
$('#target').hide();
$.ajax('/doc_'+href+'.html', {
type:'POST',
complete:function(e){
$('#target').html(e.responseText);
$('#target').fadeIn(time);
}
});
}
return false;
});
}(240));
</script>
</head>
<body>
<section id='header'>
<div class='exterior'>
<div class='interior'>
<h1>Simple Server</h1>
<div><a href='about'>About</a></div>
<div><a href='profile'>Profile</a></div>
<div><a href='contact'>Contact</a></div>
</div>
</div>
</section>
<section id='leader'>
<div class='exterior'>
<div class='fallback'></div>
<div id='target' class='interior'></div>
</div>
</section>
</body>
</html>
var http = require('http'), fs = require('fs'), url = require('url');
var server = {
root: __dirname,
port: 8080,
index: 'index.html',
except: ['get:/doc_', '/index.js']
};
var Exists = function (req, res, path) {
if (fs.existsSync(server.root + path)) {
for (var i=0; i<server.except.length; i++) {
except = server.except[i].split(':');
if (except.length > 1) {
if (req.method.match(new RegExp(except[0], 'i'))) {
if (path.match(new RegExp('^' + except[1]))) {
console.log(req.method, '\033[31mEXCEPT\033[39m', path);
return false;
}
}
} else {
if (path.match(new RegExp('^' + except[0]))) {
console.log(req.method, '\033[31mEXCEPT\033[39m', path);
return false;
}
}
}
var mtime = fs.statSync(server.root + path).mtime;
if (req.headers['if-modified-since'] != mtime) {
console.log(req.method, '\033[32m200\033[39m', path);
res.writeHead(200, {'Last-Modified': mtime});
res.end(fs.readFileSync(server.root + path));
} else {
console.log(req.method, '\033[36m304\033[39m', path);
res.writeHead(304);
res.end();
}
return true;
} else {
return false;
}
};
var Bail = function (req, res, path) {
console.log(req.method, '\033[31m404\033[39m', path);
res.writeHead(404);
res.end('Document Not Found.');
};
http.createServer(function (req, res) {
var location = url.parse(req.url);
var path = location.pathname;
if (location.pathname === '/') path += server.index
if (!Exists(req, res, path)) {
if (!Exists(req, res, path + '.html')) {
if (!Exists(req, res, path + '/' + server.index)) {
Bail(req, res, path);
}
}
}
}).listen(server.port);
console.log('Running at', server.port, '\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment