我是一个node.js新手,发现自己有点难以接受以下简单要求:
我想使用结构为的网址:
/报告/ 34eabc
...我希望这可以通过' 34aebc'路由到/public/report.html。作为我可以通过客户端Javascript(Bootstrap.js框架)访问的参数。
我的服务器设置如下:
//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' );
});
我希望在中间件框架内尽可能轻量级,但我觉得我可能错过了我应该在这里使用的范例。
答案 0 :(得分:1)
只需使用命名参数:
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....
});