Could I write app.get in express to dynamically fetch?
Instead of writing one of these for every file I want to serve could I write something to the effect of...
app.get('/' + x, function(req, res) {
res.sendfile(x + '.html');
});
If so, how would I actually do it? Can I parse x from the req before doing app.get or some开发者_如何转开发thing?
Thanks!
If you're using static files, definitely use the static folder. if you need to use sendfile try this:
app.get('/:pagename' , function(req, res) {
res.sendfile(req.params.pagename + '.html');
});
Place all your HTML files that you want to have public in a folder name public
Then add express.static
to your middleware list
app.use(express.static(__dirname + '/../public'));
To your configuration. Now these files will be routed statically. So if you have a file at location /public/foo/bar.html
then /foo/bar.html
will load and return that file.
精彩评论