Skip to content

Commit 8fc2d70

Browse files
committed
Use router from splinky framework to add express like functionality to paramify
1 parent ebd6a94 commit 8fc2d70

File tree

11 files changed

+137
-42
lines changed

11 files changed

+137
-42
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ bare
33

44
Make node.js frameworks scram - use bare
55

6-

app.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
var http = require('http')
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]};
6-
7-
http.createServer(function (req, res) {
8-
var match = paramify(req.url);
9-
10-
res.writeHead(200, {'Content-Type': 'text/html'});
11-
12-
if (match('/*')) {
13-
controller.showIndex(req, res)
14-
}
15-
1+
'use strict';
2+
var bare = require('./lib/bare')
3+
, http = require('http');
4+
5+
bare.router.on('get', '/test', function(req, res) {
6+
res.writeHead(200, {'Content-Type': 'text/plain'});
7+
res.end('Testseite\n');
8+
console.log('Testseite');
9+
});
10+
11+
// TODO: 404 callback if route does not exist
12+
http.createServer(function(req, res) {
13+
bare.router.dispatch(req, res, console.log);
1614
}).listen(8080);
17-
console.log('Server running at http://localhost:8080/');

controller.js

-20
This file was deleted.

lib/bare.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var controller = require('./controller')
2+
, bare = {
3+
router: require('./router')(),
4+
};
5+
6+
bare.router.on('get', '/admin', controller.showTest);
7+
bare.router.on('get', '/index', controller.showIndex);
8+
9+
module.exports = bare;
File renamed without changes.
File renamed without changes.

components/head.js lib/components/head.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ module.exports = react.createClass({
88
react.DOM.title(null, "React Page | Client-Server JavaScript Rendering"),
99
react.DOM.meta({ name: "viewport"
1010
, 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"
1511
})
12+
// }),
13+
// react.DOM.link({ rel: "stylesheet"
14+
// , type: "text/css"
15+
// , href: "/style.css"
16+
// })
1617
);
1718
},
1819
});

lib/controller.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
var views = require('./views')
3+
// , models = require('./models')
4+
, React = require('react');
5+
6+
module.exports.showIndex = function showIndex (req, res, routeparams) {
7+
var props = {partials:
8+
[{DOMel: React.DOM.a,
9+
href: 'asdf',
10+
subject: 'fifa'},
11+
{ DOMel: React.DOM.ul,
12+
subject:'ahoi',
13+
children: React.DOM.li(null, 'erster in der liste')},
14+
{ DOMel: React.DOM.ol,
15+
subject: 'jochen'}]
16+
};
17+
18+
views.renderView(req, res, props);
19+
};
20+
21+
module.exports.showTest= function showTest (req, res, routeparams) {
22+
var props = {partials:
23+
[{DOMel: React.DOM.a,
24+
href: 'blabal',
25+
subject: 'stream'},
26+
{ DOMel: React.DOM.ul,
27+
subject:'miami',
28+
children: React.DOM.li(null, 'hotline hotline')},
29+
{ DOMel: React.DOM.ol,
30+
subject: 'wizard'}]
31+
};
32+
33+
views.renderView(req, res, props);
34+
};

lib/router.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2013, Rod Vagg (the "Original Author")
2+
// All rights reserved.
3+
//
4+
// MIT +no-false-attribs License
5+
//
6+
// Permission is hereby granted, free of charge, to any person
7+
// obtaining a copy of this software and associated documentation
8+
// files (the "Software"), to deal in the Software without
9+
// restriction, including without limitation the rights to use,
10+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the
12+
// Software is furnished to do so, subject to the following
13+
// conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be
16+
// included in all copies or substantial portions of the Software.
17+
//
18+
// Distributions of all or part of the Software intended to be used
19+
// by the recipients as they would use the unmodified Software,
20+
// containing modifications that substantially alter, remove, or
21+
// disable functionality of the Software, outside of the documented
22+
// configuration mechanisms provided by the Software, shall be
23+
// modified such that the Original Author's bug reporting email
24+
// addresses and urls are either replaced with the contact information
25+
// of the parties responsible for the changes, or removed entirely.
26+
//
27+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34+
// OTHER DEALINGS IN THE SOFTWARE.
35+
//
36+
// Except where noted, this license applies to any and all software
37+
// programs and associated documentation files created by the
38+
// Original Author, when distributed with the Software.
39+
40+
const paramify = require('paramify')
41+
42+
function Router () {
43+
if (!(this instanceof Router))
44+
return new Router()
45+
46+
this._routes = {}
47+
}
48+
49+
Router.prototype.on = function (method, route, handler) {
50+
var _m = method.toLowerCase()
51+
if (!this._routes[_m])
52+
this._routes[_m] = []
53+
this._routes[_m].push({ route: route, handler: handler })
54+
}
55+
56+
Router.prototype.dispatch = function (req, res, callback) {
57+
var method = req.method.toLowerCase()
58+
, url = (req.url || '').replace(/\?.*$/, '')
59+
, match = paramify(url)
60+
, routes = this._routes[method]
61+
, i
62+
63+
if (routes) {
64+
for(i = 0; i < routes.length; i++) {
65+
if (match(routes[i].route))
66+
return routes[i].handler(req, res, match.params)
67+
}
68+
}
69+
70+
callback(new Error('not found'))
71+
}
72+
73+
module.exports = Router

views.js lib/views.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
var react = require('react')
23
, document = require('./components/document');
34

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"url": "https://github.com/dkolba/bare/issues"
2626
},
2727
"dependencies": {
28-
"react": "~0.11.0"
28+
"react": "^0.11.0",
29+
"paramify": "^0.1.2"
2930
}
3031
}

0 commit comments

Comments
 (0)