如何在nodejs中删除index.html的首选项?

时间:2016-04-03 06:19:20

标签: node.js

在我的公共文件夹中,我有index.html文件,我的路由处理程序就像这样

 router.get('/', function (req, res, next) {
   // res.send('index.html');
    if (req.user)
        res.redirect('home');
    else
        res.redirect('login');
 });

正如您所看到的,我已经注释掉了index.html文件的服务,但是nodejs仍然从公共目录提供index.html,而不是重定向到home或login。 但是,如果我删除/重命名index.html文件,那么它可以正常工作

那么如何配置nodejs以便它根据请求调用路由处理程序而不是服务索引文件?

2 个答案:

答案 0 :(得分:2)

这是由于app.use的订购,之前是

app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);

将此更改为

app.use('/', index);
app.use(express.static(path.join(__dirname, 'public')));

解决问题。

答案 1 :(得分:2)

express.static() middleware包含use strict; use warnings; # need this to send emails via SMTP server use Net::SMTP; use File::Glob; # SMTP Server Name - PHX-PDC SMTP my $sSMTPServerName = 'name-of-server.working.checked'; # print usage error if ( $#ARGV != 0 ) { print "Usage Error: scriptname.pl <HRM|FIN> \n"; exit 4; } if ( $ARGV[0] ne "XXX" && $ARGV[0] ne "XXX" ){ print "ERROR: Invalid argument.\n"; print "Usage Error: scriptname.pl <XXX|XXX> \n"; exit 5; } # get environment from command line my $sApp = $ARGV[0]; # get todays date in YYYYMMDD format my @aToday=localtime(time); # get the date in yyyy.mm.dd format my $sToday = sprintf ("%04d.%02d.%02d", $aToday[5]+1900, $aToday[4]+1, $aToday[3]); # getting the env variables from bat file my $sScriptName = "name-of-script"; # make path to log file my $sOutLogFile = "$sXXLOG\\$sApp$sScriptName.$sToday.log"; # open logfile for writing open(OUTLOG, "> $sOutLogFile") or die("Can't open $sOutLogFile\n"); if ( $sApp eq "XXX" ) { # print to log file print OUTLOG "Starting \n"; # search for the existence of most recent subfolder opendir(DIRLIST, $svar2) or die "Can't open $svar2: $!"; my @adirs = readdir(DIRLIST); closedir(DIRLIST); my @aRecentdir = grep(/$sToday/, @adirs); if ( @aRecentdir && $aRecentdir[0] ne " " ) { # found the recent dir, now calling sub my @alist = ($svar1, $svar2, $aRecentdir[0] $svar3, $svar4); ABC(@alist); ## send the email code {Tested to be working fine} #---- End SMTP emailer code ---- } } sub ABC { my (list of variables) = @_; print OUTLOG "Found the latest dir!\n"; # search for the Successful String in the logfile my $slogfile = "$svar\\$svar\\...\\xyz.log"; if ( -e $slogfile ) { print OUTLOG "Found the log file. \n"; open(LOG,"$slogfile") or die("Can't open $slogfile\n"); my $sLines; { local $/ = undef; $sLines = <LOG>; } while ( <LOG> ) { if ( m/^(\d\d) (\w{3}) (\d{4}) (\d\d):(\d\d):(\d\d) .*/ ) { #### NEED HELP IN THIS PART ####### my $var = "$1$2$3$4$5$6"; print "$var\n"; } } close(LOG); } return 1; } ################################ # Send Email via SMTP function ################################ sub sendEmail { ## SMTP code.. tested to be working fine } 选项,可让您更改默认文件名。

或者,出于您的意图,完全禁用该功能:

index