node.js - Dynamic routing based on file system directory search -
i want route html page without having '.html' in url. trying creating custom router looks through directory , checks see if there file extension '.html', if there route page.
i need allow router fall through '/api'
route if url contains '/api'. want allow users html files without having type file extension , keep other routing intact.
var express = require('express'); var path = require('path'); var fs = require('fs'); var router = express.router(); var app = express(); router.route('/:id') .get(function(req, res) { fs.readdir('public/', function(err, items) { (var i=0; i<items.length; i++) { if (req.params.id == path.basename(items[i])) { app.get(function(req, res) { res.render(items[i]); }); } } }); }); router.route('/api') .get(function (req, res) { ... }) .post(function (req, res) { ... }); app.use('/', router);
i tried using express.static
not working either.
fs.readdir('public/', function(err, items) { (var i=0; i<items.length; i++) { if (path.extname(items[i]) == '.html') { app.use('/' + path.basename(items[i], '.html'), express.static(items[i])); } } });
Comments
Post a Comment