Skip to content

Instantly share code, notes, and snippets.

@nemtsov
Last active December 20, 2015 11:59
Show Gist options
  • Save nemtsov/6127194 to your computer and use it in GitHub Desktop.
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)
//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)
}
body {
font-family: Verdana, Geneva, sans-serif;
padding: 1em;
background: #D1DFE8;
}
.container {
color: white;
padding: 2em;
background-color: #41546F;
}
<!doctype html>
<html>
<head>
<title></title>
<link />
</head>
<body>
<div class="container">
</div>
<script></script>
</body>
</html>
console.log('awesome')
@nemtsov
Copy link
Author

nemtsov commented Dec 17, 2013

Note to self: you can use substack/hyperspace now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment