Skip to content

Instantly share code, notes, and snippets.

@ogorzalka
Created October 12, 2011 09:26
Show Gist options
  • Save ogorzalka/1280737 to your computer and use it in GitHub Desktop.
Save ogorzalka/1280737 to your computer and use it in GitHub Desktop.
Node files are redirected to the right node app port
# This is the .htaccess file in our document root.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SERVER_PORT} !^3000$ # edit the 3000 port with your node app port
RewriteCond %{REQUEST_URI} .*\.(styl|coffee|less) # stylus, coffeescript and less files are managed
RewriteRule (.*) http://%{HTTP_HOST}:3000%{REQUEST_URI} [P] # edit the 3000 port with your node app port
</IfModule>
var ejs = require("ejs"),
fs = require("fs"),
express = require("express"),
less = require("less"),
stylus = require("stylus"),
coffee = require("coffee-script"),
nib = require('nib'),
app = express.createServer();
function generate404(req) {
var htmlErrorTpl = "<!DOCTYPE html>";
htmlErrorTpl += "<html>";
htmlErrorTpl += "<head>";
htmlErrorTpl += "<title>404 Not Found</title>";
htmlErrorTpl += "</head>";
htmlErrorTpl += "<body>";
htmlErrorTpl += "<h1>Not Found</h1>";
htmlErrorTpl += "<p>The requested URL ${path} was not found on this server.</p>";
htmlErrorTpl += "<hr>";
htmlErrorTpl += "<address>Node App at ${host} Port ${port}</address>";
htmlErrorTpl += "</body>";
htmlErrorTpl += "</html>";
var host = req.headers.host.split(':');
return htmlErrorTpl.replace('${path}', __dirname+req.url)
.replace('${host}', host[0])
.replace('${port}', host[1])
}
app.configure(function() {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.get("*.less", function(req, res) {
var path = __dirname + req.url;
fs.readFile(path, "utf8", function(err, data) {
if (err) {
console.log(err);
res.statusCode = 404
res.header("Status", "404 Not Found");
res.header("Content-type", "text/html");
res.send(generate404(req));
} else {
var parser = new(less.Parser)({
paths: [
__dirname + req.url.substr(0,req.url.lastIndexOf('/')) // current path
], // Specify search paths for @import directives
});
parser.parse(data, function(err, css) {
if (err) {
console.log(err);
res.header("Content-type", "text/html");
res.send('<b>Syntax Error&nbsp;:</b><br />'+err);
}else {
res.header("Content-type", "text/css");
res.send(css.toCSS());
}
});
}
});
});
app.get("*.coffee", function(req, res) {
var path = __dirname + req.url;
fs.readFile(path, "utf8", function(err, data) {
if (err) {
console.log(err);
res.statusCode = 404
res.header("Status", "404 Not Found");
res.header("Content-type", "text/html");
res.send(generate404(req));
} else {
try {
js = coffee.compile(data);
res.header("Content-type", "text/javascript");
res.send(js);
} catch(err) {
console.log(err);
res.header("Content-type", "text/html");
res.send('<b>Syntax Error&nbsp;:</b><br />'+err);
}
}
});
});
app.get("*.styl", function(req, res) {
var path = __dirname + req.url;
fs.readFile(path, "utf8", function(err, data) {
if (err) {
console.log(err);
res.statusCode = 404
res.header("Status", "404 Not Found");
res.header("Content-type", "text/html");
res.send(generate404(req));
} else {
var paths = [
__dirname + req.url.substr(0,req.url.lastIndexOf('/')) // current path
];
console.log(paths);
stylus(data)
.set('paths', paths)
.use(nib())
.import('nib')
.render(function(err, css){
if (err) {
console.log(err);
res.header("Content-type", "text/html");
res.send('<b>Syntax Error&nbsp;:</b><br />'+err);
}else {
res.header("Content-type", "text/css");
res.send(css);
}
});
}
});
});
})
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment