2016-12-12 49 views
0

我有我正在配置通快递快递优先匹配规则

通天服务器

我想,我的静态文件静态地提供,所以我增加了以下规定:

app.use('/', express.static('public')); 

app.get(/.*\.(png|jpg|js|css)$/, express.static('public')); 

然而我也想在页面被适当地装上刷新/直接链接访问,所以我增加了以下规则

app.get('/*', function (request, response) { // This wildcard method handles all requests 
    response.sendFile(path.resolve(__dirname, 'public', 'index.html')) 
}); 

不过,现在在页面加载,当页面请求我的bundle.js服务器正在为它提供index.html文件(即使它被前面的静态文件规则所捕获)。我认为这是事实,因为它以某种方式优先处理规则。我如何消除该规则中的静态文件?上下文

全部文件:

import express from 'express'; 
import path from 'path'; 
import helmet from 'helmet'; 
import session from 'express-session'; 

const port = process.env.PORT || 8080; 
const app = express(); 

// we use helmet to disable typically unsafe operations 
app.use(helmet()); 

// COOKIE CONFIGURATION 

// we do not use the default session cookie name 
// this is done to avoid attackers from fingerprinting the server 
// and targetting it accordingly 
app.set('trust proxy', 1); // trust first proxy 
app.use(session({ 
    secret: 'sercret', 
    name: 'name' // the name of the sesion ID cookie to set in the response and read from the request 
})); 

// ROUTE CONFIGURATION 

app.use('/', express.static('public')); 

app.get(/.*\.(png|jpg|js|css)$/, express.static('public')); 

app.get('/*', function (request, response) { // This wildcard method handles all requests 
    response.sendFile(path.resolve(__dirname, 'public', 'index.html')) 
}); 

app.listen(port,() => { 
    console.log(`Server is now listening on port ${port}`); 
}); 
+0

的路由根据其定义的顺序优先。你在静态处理器后面定义了'/ *'*的路由吗? – str

+0

是的。他们在问题 – mangusbrother

+0

的顺序中定义了添加完整文件的可见性 – mangusbrother

回答

1

正则表达式是没有相应的有效途径expresspath-to-regexp源。和更新“/”路线:

app.use(express.static('public')); 
+0

即使如此,'GET/bundle.js',会被'app.use('/',express.static('public' ))',考虑到'public.'中存在'bundle.js'。不是吗? –

+0

@SergeyLapin我认为,'/'只适用于根网址。 –

+0

yes'/'仅适用于根目录 – mangusbrother