在我的nodejs应用程序中,我将静态目录设置为:
app.use(express.static(__dirname + '/mydir'));
在mydir目录中,我有一个index.html。当我在上面的线下方有一条路线时,我注意到了:
app.get('/', function(req, res, next) {
console.log("here")
res.send('test');
});
虽然index.html确实出现了,但它没有做任何事情!我希望能够在它返回之前做一些事情。所以,我有:
app.use(function(req, res) {
console.log("do something here");
});
然而,“在这里做点什么”永远不会被解雇。我想在做某事之前检查请求的标题。我需要做什么?请注意,我之后有一堆图像,我真的不希望console.log(“here”)或“在这里做点什么”被多次记录,我只想在用户加载时执行一次“ index.html“或调用任何特定路线。
答案 0 :(得分:2)
您需要做的就是在 <?php
# original function
function process_line($input) {
$reg_exUrl = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
if(preg_match($reg_exUrl, $input, $url)) {
for ($i = 0; $i < count($url); $i++) {
$input = preg_replace($reg_exUrl, "[" . $i . "]", $input);
}
}
return $input;
}
# function with fixes incorporated
function process_line1($input) {
$reg_exUrl = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
if(preg_match_all($reg_exUrl, $input, $url)) {
$url_matches = $url[0];
for ($i = 0; $i < count($url_matches); $i++) {
echo $i;
# add explicit limit of 1 to arguments of preg_replace
$input = preg_replace($reg_exUrl, "[" . $i . "]", $input, 1);
}
}
return $input;
}
$input = "test https://www.google.com/ mmh http://stackoverflow.com/";
$input = process_line1($input);
echo $input;
?>
中间件之前放置自定义中间件。例如:
express.static()
或者,如果您想将其限制在特定路径:
app.use(function(req, res, next) {
console.log('do something here');
next(); // Continue on to the next middleware/route handler
});
app.use(express.static(__dirname + '/mydir'));