Skip to content

Commit ebd6a94

Browse files
committed
Started minimal MVC architecture
1 parent 58f064c commit ebd6a94

File tree

8 files changed

+88
-12
lines changed

8 files changed

+88
-12
lines changed

.jshintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"node": true
3+
, "laxcomma": true
4+
, "validthis": true
5+
}

app.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
var fs = require('fs')
21
var http = require('http')
3-
var paramify = require('paramify')
2+
, paramify = require('paramify')
3+
, controller = require('./controller');
4+
5+
// var props = {partials: [react.DOM.div, react.DOM.img, react.DOM.a, react.DOM.ol]};
46

57
http.createServer(function (req, res) {
6-
var match = paramify(req.url)
8+
var match = paramify(req.url);
79

8-
res.writeHead(200, {'Content-Type': 'text/plain'})
10+
res.writeHead(200, {'Content-Type': 'text/html'});
911

1012
if (match('/*')) {
11-
helloWorld(match.params, req, res)
13+
controller.showIndex(req, res)
1214
}
1315

14-
}).listen(8080, '127.0.0.1')
15-
16-
function helloWorld(params, req, res) {
17-
res.end('Hello World')
18-
}
19-
20-
console.log('Server running at http://localhost:8080/')
16+
}).listen(8080);
17+
console.log('Server running at http://localhost:8080/');

components/body.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var react = require('react');
2+
3+
module.exports = react.createClass({
4+
5+
render: function() {
6+
return react.DOM.body({
7+
children: this.props.partials.map(function(partial) {
8+
return partial.DOMel(null, partial.subject, partial.children);
9+
})
10+
});
11+
},
12+
});

components/document.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var react = require('react');
2+
var head = require('./head');
3+
var body = require('./body');
4+
5+
module.exports = react.createClass({
6+
7+
render: function() {
8+
return react.DOM.html(null,
9+
head(null),
10+
body(this.props)
11+
);
12+
},
13+
});

components/head.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var react = require('react');
2+
3+
module.exports = react.createClass({
4+
5+
render: function() {
6+
return react.DOM.head(null,
7+
react.DOM.meta({charSet: "UTF-8"}),
8+
react.DOM.title(null, "React Page | Client-Server JavaScript Rendering"),
9+
react.DOM.meta({ name: "viewport"
10+
, content: "width=device-width, initial-scale=1.0, user-scalable=no"
11+
}),
12+
react.DOM.link({ rel: "stylesheet"
13+
, type: "text/css"
14+
, href: "/style.css"
15+
})
16+
);
17+
},
18+
});

controller.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var views = require('./views')
2+
, React = require('react');
3+
4+
module.exports.showIndex = function showIndex (req, res) {
5+
var props = {partials:
6+
[{DOMel: React.DOM.a,
7+
href: 'asdf',
8+
subject: 'fifa'},
9+
{ DOMel: React.DOM.ul,
10+
subject:'ahoi',
11+
children: React.DOM.li(null, 'erster in der liste')},
12+
{ DOMel: React.DOM.ol,
13+
subject: 'jochen'}]
14+
};
15+
16+
views.renderView(req, res, props);
17+
18+
// models.getPageObj(this.req, this.res, 'index', mappings.index,
19+
// views.renderView);
20+
};

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@
2323
"license": "BSD-2-Clause",
2424
"bugs": {
2525
"url": "https://github.com/dkolba/bare/issues"
26+
},
27+
"dependencies": {
28+
"react": "~0.11.0"
2629
}
2730
}

views.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var react = require('react')
2+
, document = require('./components/document');
3+
4+
module.exports.renderView = function renderView(req, res, props) {
5+
var html = react.renderComponentToString(document(props));
6+
7+
res.end(html);
8+
}

0 commit comments

Comments
 (0)