Skip to content

Instantly share code, notes, and snippets.

@imkost
Last active November 18, 2016 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imkost/6b872729f121cb1f3067caff28f26675 to your computer and use it in GitHub Desktop.
Save imkost/6b872729f121cb1f3067caff28f26675 to your computer and use it in GitHub Desktop.
// server.js
const http = require('http');
const fs = require('fs');
require('./components');
const PORT = 4000;
const styles = fs.readFileSync('./styles.css');
const components = fs.readFileSync('./components.js');
http.createServer(server).listen(PORT);
function server(req, res) {
const { url } = req;
res.end(
url === '/styles.css' ? styles :
url === '/components.js' ? components :
App({ url }) // App is in global namespace (global.App)
);
}
// components.js
function App({ url }) {
return `
<!doctype html>
<html class="app">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="/styles.css" />
</head>
<body class="app__body">
Nothing here yet, just a box: ${Box()}
<div class="app__scripts">
<script>window.global = window;</script>
<script src="/components.js"></script>
</div>
</body>
</html>
`
}
function Box() {
return `
<div class="box"></div>
`;
}
global.App = App;
global.Box = Box;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment