I'm a node.js newbie and finding myself a bit stumped with the following simple requirement:
I want to use a url with the structure:
/report/34eabc
...where I want this to route to /public/report.html with '34aebc' as a parameter that I can access via client-side Javascript (Bootstrap.js framework).
My server is set up like this:
//Create server
var app = express();
// Configure server
app.configure( function() {
//parses request body and populates request.body
app.use( express.bodyParser() );
//checks request.body for HTTP method overrides
app.use( express.methodOverride() );
//perform route lookup based on url and HTTP method
app.use( app.router );
//Where to serve static content
app.use( express.static( path.join( application_root, 'public') ) );
//Show all errors in development
app.use( express.errorHandler({ dumpExceptions: true, showStack: true }));
});
//Start server
var port = 4711;
var server = require('http').createServer(app), io =require('socket.io').listen(server);
server.listen( port, function() {
console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});
// Routes
app.get( '/api', function( request, response ) {
response.send( 'API is running' );
});
I'd like to be as lightweight on the middleware frameworky stuff as possible, but I sense that I am maybe missing the paradigm I should be using here.
Just use named parameters:
app.get( '/report/:id', function(req,res){
var id = req.params.id;
// id gets the value '34eabc' (or whatever is passed in in the URL)
// do your stuff....
});