Last active
December 20, 2015 11:59
-
-
Save nemtsov/6127194 to your computer and use it in GitHub Desktop.
An example of a simple server using trumpet (for html templating) and nap (for asset mgmt)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//process.env.NODE_ENV = 'production' | |
var express = require('express') | |
, fs = require('fs') | |
, path = require('path') | |
, trumpet = require('trumpet') | |
, nap = require('nap') | |
, app = express() | |
nap({ | |
mode: process.env.NODE_ENV | |
, assets: { | |
css: {all: ['/public/css/*']} | |
, js: {all: ['/public/js/*']} | |
} | |
}) | |
app.configure('development', function () { app.use(nap.middleware) }) | |
app.configure('production', function () { nap.package() }) | |
app.configure(function () { | |
app.use(app.router) | |
app.use(express.static(__dirname + '/public')) | |
}) | |
app.get('/', function (req, res, next) { | |
var tr = trumpet() | |
tr.createWriteStream('link', {outer: true}).end(nap.css('all')) | |
tr.createWriteStream('script', {outer: true}).end(nap.js('all')) | |
tr.createWriteStream('title').end('Awesome!') | |
tr.createWriteStream('.container').end('Working well.') | |
res.writeHead(200, {'Content-Type': 'text/html'}) | |
getPageStream('index').pipe(tr).pipe(res) | |
}) | |
app.listen(3000) | |
function getPageStream(name) { | |
var fname = path.join(__dirname, 'public' | |
, 'html', name + '.html') | |
return fs.createReadStream(fname) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
font-family: Verdana, Geneva, sans-serif; | |
padding: 1em; | |
background: #D1DFE8; | |
} | |
.container { | |
color: white; | |
padding: 2em; | |
background-color: #41546F; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title></title> | |
<link /> | |
</head> | |
<body> | |
<div class="container"> | |
</div> | |
<script></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('awesome') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note to self: you can use substack/hyperspace now.