Skip to content

Commit

Permalink
Update dynapack, provide a real secret reward
Browse files Browse the repository at this point in the history
  • Loading branch information
bauerca committed Mar 2, 2015
1 parent e1d0d1b commit b98ad21
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules/
chunks/
bundles/
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ To run the example, issue the following commands
> git clone https://github.com/bauerca/dynapack-example-simple.git
> cd dynapack-example-simple
> npm install
> ./node_modules/.bin/dynapack client.js
> ./node_modules/.bin/dynapack app.js
> node server.js
```

Then point your browser to `http://localhost:3333`, and start clicking
on the list items to look for the secret.

If you are using Chrome, fire up the dev console and switch to the
Fire up dev console in Chrome or Firebug in Firefox and switch to the
"Network" tab to watch the bundles come in as they are needed.

# License

MIT
6 changes: 3 additions & 3 deletions client.js → app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var router = require('./router');
var route = require('./route');

function onChange(path) {
router(path, function(msg) {
document.getElementById('msg').innerHTML = msg;
route(path, function(err, content) {
document.getElementById('content').innerHTML = err ? err.message : content;
});
}

Expand Down
Binary file added images/secret.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion nope.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = 'You didn\'t find the secret. :(';
module.exports = '<h1>You didn\'t find the secret. :(</h1>';
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "build.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
"start": "node_modules/.bin/dynapack app.js && node server.js"
},
"keywords": [
"dynapack"
Expand All @@ -14,8 +14,8 @@
"license": "MIT",
"dependencies": {
"connect": "^2.18.0",
"dynafetch": "0.0.3",
"dynapack": "^0.2.2",
"dynapack": "^0.3.x",
"node-ensure": "0.0.0",
"parseurl": "^1.0.1",
"serve-static": "^1.2.0"
},
Expand Down
21 changes: 21 additions & 0 deletions route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var ensure = require('node-ensure');

var secret = './secret' /*js*/;
var nope = './nope' /*js*/;

function route(path, render) {
var page = (path === '/secret') ? secret : nope;

ensure([page], function(err) {
var html;

if (!err) {
// 'page' is an html string.
html = require(page);
}

render(err, html);
});
}

module.exports = route;
12 changes: 0 additions & 12 deletions router.js

This file was deleted.

5 changes: 4 additions & 1 deletion secret.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
module.exports = 'You found the secret page!';
module.exports = (
'<h1>You found the secret page!</h1>' +
'<img src="/images/secret.png"></img>'
);
27 changes: 17 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
var connect = require('connect');
var serveStatic = require('serve-static');
var fs = require('fs');
var router = require('./router');
var route = require('./route');
var parseurl = require('parseurl');

var words = 'hey do you want to know a secret';
var index = function(msg) {

function renderString(content) {
return (
'<html>' +
'<head></head>' +
'<head>' +
'<style>li {cursor: pointer;} li:hover {color: #3366bb;}</style>' +
'</head>' +
'<body>' +
'<h1 id="msg">' + msg + '</h1>' +
'<h1>Find the secret</h1>' +
'<ul>' +
words.split(' ').map(function(word) {
return '<li onclick="go(\'/' + word + '\')">secret</li>';
}).join('') +
'</ul>' +
'<div id="content">' + content + '</h1>' +
'<script src="/entry.0.js"></script>' +
'<script src="/1.js"></script>' +
'</body>' +
'</html>'
);
};
}

var app = connect();
app.use(serveStatic(__dirname + '/dynapack-chunks'));
app.use(serveStatic(__dirname + '/bundles'));
app.use('/images', serveStatic(__dirname + '/images'));
app.use(function(req, res) {
router(parseurl(req).path, function(msg) {
var html = index(msg);
route(parseurl(req).path, function(err, content) {
var html = renderString(err ? err.message : content);
res.end(html);
});
});
app.listen(3333);
app.listen(3333, function(err) {
if (err) throw err;
console.log('Go to http://localhost:3333');
});

0 comments on commit b98ad21

Please sign in to comment.