Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js Express压缩脚本_Javascript_Node.js_Express - Fatal编程技术网

Javascript Node.js Express压缩脚本

Javascript Node.js Express压缩脚本,javascript,node.js,express,Javascript,Node.js,Express,我目前正在使用app.get解析我工作的网站的URL请求。我正在使用app.get完成以下任务: www.site.com/html/contact.html 而是翻译成 www.site.com/contact 我使用app.get的方式如下: app.get('/team',function(req,res){res.sendfile('team.html',{'root':'./html'});}); app.get('/yada',function(req,res){res.sendfi

我目前正在使用app.get解析我工作的网站的URL请求。我正在使用app.get完成以下任务:

www.site.com/html/contact.html

而是翻译成

www.site.com/contact

我使用app.get的方式如下:

app.get('/team',function(req,res){res.sendfile('team.html',{'root':'./html'});});
app.get('/yada',function(req,res){res.sendfile('yada.html',{'root':'./html'});});
app.get('/home',function(req,res){res.redirect('/');});
app.get('*',function(req, res){res.redirect('/');});
这一切都非常出色,我的问题可能是一些特定于JavaScript的东西。我想要的是这样的东西:

app.get({
   '/team',function() ...,
   '/home',function() ...,
   '/yada',function()...
});
类似于我可以做的:

var express = requires('express'),
    app = express();
这可能吗

更新

我将errorHandler与CuriousGuy的解决方案结合在一起,效果非常好:

errorHandler = function(err,req,res,next){res.redirect('/');};
app.get('/:page',function(req,res){res.sendFile(req.params.page + '.html',{'root':'./html'});}).use(errorHandler);

虽然我确实必须更改文件名以适合这种路由方法,但到目前为止,这种方法工作得非常出色。

您可以执行以下操作:

var routes = {
  '/team': function() {},
  '/home', function() {}
};
for (path in routes) {
  app.get(path, routes[path]);
}

虽然你应该小心这样的微优化。使用标准语法可以保持代码干净易读。在牺牲易读性的同时,刻意使代码“更小”并不总是一件好事

如果您只需要编写更少的代码来生成“非点html”URL,则可以使用正则表达式:

app.get(/^\/(team|yada|contact)\/?$/, function(req, res) {
    res.sendfile('./html/' + req.params[0] + '.html');
});
app.get('*',function(req, res){res.redirect('/');});
app.get('/:page', function(req, res, next) {
    if (['team', 'yada', 'contact'].indexOf(req.params.page) === -1) {
        return next();
    }
    res.sendfile('./html/' + req.params.page + '.html');
});
团队| yada |联系人
部分上方是一个页面列表,页面之间用
分隔。您可以在此列表中拥有任意数量的页面,但请记住,正则表达式可能会影响应用程序的性能

更新:

经过一段时间的编码,我找到了一个没有正则表达式的解决方案:

app.get(/^\/(team|yada|contact)\/?$/, function(req, res) {
    res.sendfile('./html/' + req.params[0] + '.html');
});
app.get('*',function(req, res){res.redirect('/');});
app.get('/:page', function(req, res, next) {
    if (['team', 'yada', 'contact'].indexOf(req.params.page) === -1) {
        return next();
    }
    res.sendfile('./html/' + req.params.page + '.html');
});
只需将您需要的页面添加到
['team','yada','contact']
数组中即可

更新2:

为了演示正则表达式的性能,我编写了一个小脚本,它以两种不同的方式实现相同的功能——第一种情况是用正则表达式替换字符串,另一种情况是不使用正则表达式:

var sentence = 'In order to consolidate your success we advise you to continue your work';

for (var i = 0; i < 1000000; i++) {
    //case 1: regular expressions replacement
    var word = sentence.replace(/success/, 'SUCCESS');

    //case 2: simple string replacement
    //var word = sentence.replace('success', 'SUCCESS');
}
当案例1未注释且案例2已注释时,我得到的是
0m1.106s
,否则--
0m0.585s
。因此,正则表达式的运行速度是非正则表达式的两倍。这就是为什么建议尽可能避免在性能敏感的应用程序中使用正则表达式


我希望我回答了你的问题,并且对我的英语感到抱歉:“谢谢你的输入,我没有考虑到微优化(正是我要做的事情)会阻碍我的努力。好奇的家伙,我已经成功地实现了你提到的两种方式,我很好奇为什么使用正则表达式会影响性能。我真的很欣赏最后一个使用RegEx的时间效率示例,我从来没有想过用这种方式测试,现在我知道了两个新的东西。你确实回答了我的问题,谢谢!:)