I'm working my way through Node for Front End devs, and as people on SO have already pointed out, Connect no longer has a module for routing. Some people have advised using Express, but I'm unsure of the exact syntax.
The example I am working through is hosted here:
github.com/garann/node-for-frontend-devs/blob/master/03-03.js
I would in turn like to finish the tutorial on templating:
js: github.com/garann/node-for-frontend-devs/blob/master/04-02.js
html: github.com/garann/node-for-frontend-devs/blob/master/public/parent.html
and was wondering whether people think it might be better to ditch these Connect based tutes and just learn how to use Express? As routing will most likely require Express anyway..
Thanks to SO excellent spam protection, I've had to remove the https:// portion of the github links.
Thanks for any help.
Try use this: https://github.com/baryshev/connect-route
UPDATE
In your project folder execute:
npm install connect-route
Updated code from your example:
var connect = require("connect");
var connectRoute = require("connect-route");
connect(
connect.static(__dirname + "/public"),
connectRoute(function(app) {
app.get("/sayHello/:firstName/:lastName", function(req, res) {
var userName = req.params.firstName + " " + req.params.lastName,
html = "<!doctype html>" +
"<html><head><title>Hello " + userName + "</title></head>" +
"<body><h1>Hello, " + userName + "!</h1></body></html>";
res.end(html);
});
})
).listen(8000);
Point your browser to:
http://[your_host_here]:8000/sayHello/nick/name
connect.router was removed in version 2.x. I took the code from 1.x and I published it as connect_router
.
npm install --save connect_router
if (!connect.router) {
connect.router = require('connect_router');
}
function route(rest) {
rest.get('/api/hello', function (req, res) {
res.end('hello');
});
}
app = connect()
.use(connect.router(route))
;
server = app.listen(port, function () {
console.log('listening on', server.address());
});
https://github.com/coolaj86/connect_router taken from Connect 1.x's router with documentation essentially being the tests
The original documentation is... somewhere.