router.get()怎么匹配带点的请求
发布于 22 天前 作者 muzi-xiangxiang 287 次浏览 来自 问答

http://localhost:88/index.html
这样的请求后台配置router.get(’/index.html’)显示404 配置router.get(’/index’)还是404

6 回复

你要配置静态资源目录

@Binaryify 客户非要在地址栏输入index.html 我让他跳转到登录页,但是捕捉不到

我这样写是可以匹配上的,你看看是不是你route的顺序没对

router.get('/index.html', function (req, res, next) {
  res.redirect("/login");
});

@Soontao 谢了 确实是顺序出现了问题 再问你个问题呗 我配置了 image.png然后我的image.png每次触发就卡在这里了,是什么原因啊

@muzi-xiangxiang 卡住的话一般是前面匹配上了没有next,这两段代码看不出什么问题

@Soontao

var express = require('express');
var path = require('path');
var log4js = require('log4js');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var session = require('express-session');
var RedisStore=require('connect-redis')(session);
var favicon = require('serve-favicon')
var routes = require('./routes/index');

var app = express();
/*跨域处理*/
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  next();
});

/*日志*/

exports.logger = function (name) {
  var logger = log4js.getLogger(name);
  logger.setLevel('info');
  return logger;
}

log4js.configure({
  "appenders": [{
    type: 'logLevelFilter',
    level: 'INFO',
    appender: {
      type: 'console',
      category: 'zdy'
    }
  }, {
    type: 'logLevelFilter',
    level: 'INFO',
    appender: {
      type: 'dateFile',
      filename: 'logs/',
      pattern: 'yyyyMMdd-info.log',
      absolute: true,
      alwaysIncludePattern: true
    }
  }],
  replaceConsole: true
});
app.use(log4js.connectLogger(this.logger('zdy'), {level: 'auto', format: ':method :url'}));

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon(path.join(__dirname, 'public/img', 'favicon.ico')));
app.use(bodyParser.json({limit: '2mb'}));
app.use(bodyParser.urlencoded({limit: '2mb',extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(flash());

// 本地redis配置参数
var options={
  host:'127.0.0.1',
  port:'6379',
  db:0,
  ttl:60*15
}
app.use(session({
  resave: false,
  saveUninitialized: true,
  secret: 'gsjs',
  store: new RedisStore(options)
}));


app.use(function (req, res, next) {
  var error = req.flash('error');
  res.locals.error = error.length ? error : null;

  var success = req.flash('success');
  res.locals.success = success.length ? success : null;
  next();
});

app.use('/', routes);

app.use(function (req, res, next) {
  var err = new Error('Not Found');
  console.log('请求:'+req.url+' 404');
  err.status = 404;
  //res.render('404');
  next(err);
});

if (app.get('env') === 'development') {
  app.use(function (err, req, res) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

app.use(function (err, req, res) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

var port = 88;
app.set('port', port);

if (!module.parent) {
  app.listen(port);
  console.log("启动服务器, 开始监听'%d'端口, 以'%s'模式运行.", app.settings.port, app.settings.env);
}

app.on('error', onError);
app.on('listening', onListening);

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = (typeof port === 'string') ? 'Pipe ' + port : 'Port ' + port;
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}
var debug = require('debug')('gsjs:server');
function onListening() {
  var addr = server.address();
  var bind = (typeof addr === 'string') ? 'pipe ' + addr : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

module.exports = app;
回到顶部