Nodejs эквивалент этого .htaccess
Возможно ли построить такой код в node.js
?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond% {REQUEST_URI}! / (View) / [NC]
RewriteCond% {REQUEST_FILENAME}!-F
RewriteRule ^ (. *) $ Index.html [L, QSA]
</IfModule>
URL-адрес отображения маршрута не является "представлением", а также файл не существует, тогда напишите index.html
.
используя что-то вроде express
или connect
UPDATE: мне нужно регулярное выражение для !/(view)/
в маршруте для express
в node.js
.
Ответы
Ответ 1
Вы пробовали:
- Служить статике
- URL поиска/просмотра
-
Поймать все остальное
app.configure(function(){
app.use(express.static(__dirname+'/public')); // Catch static files
app.use(app.routes);
});
// Catch /view and do whatever you like
app.all('/view', function(req, res) {
});
// Catch everything else and redirect to /index.html
// Of course you could send the file content with fs.readFile to avoid
// using redirects
app.all('*', function(req, res) {
res.redirect('/index.html');
});
ИЛИ
- Служить статике
-
Проверьте, есть ли URL-адрес/view
app.configure(function(){
app.use(express.static(__dirname+'/public')); // Catch static files
app.use(function(req, res, next) {
if (req.url == '/view') {
next();
} else {
res.redirect('/index.html');
}
});
});
ИЛИ
Ответ 2
Структура finally:
var express = require('express'), url = require('url');
var app = express();
app.use(function(req, res, next) {
console.log('%s %s', req.method, req.url);
next();
});
app.configure(function() {
var pub_dir = __dirname + '/public';
app.set('port', process.env.PORT || 8080);
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.static(pub_dir));
app.use(app.router);
});
app.get('/*', function(req, res) {
if (req.xhr) {
var pathname = url.parse(req.url).pathname;
res.sendfile('index.html', {root: __dirname + '/public' + pathname});
} else {
res.render('index');
}
});
app.listen(app.get('port'));
Спасибо всем.
PD: рендеринг html с модулем ejs