在FeathersJS中使用connect-history-api-fallback中间件来服务Vue.js SPA

正如标题所示,我正在为FeathersJS中的public/index.html提供一个Vue.js单页应用程序。 由于我使用的是vue-router的HTML5历史logging模式,为了将请求的前端位置重写为/index.html ,我必须使用像connect-history-api-fallback这样的中间件。

app.use(notFound());之前在src/middleware设置这个中间件app.use(notFound()); 不起作用,因为Feathers在一切之前提供静态文件,所以在请求被重写的时候什么也没有提起/index.htmlnotFound提供404响应。

src/app.js .use('/', serveStatic(app.get('public')))也是有问题的,因为它会将每个服务请求重写到/index.html ,导致API调用不可用。

我结束了移动serveStatic中间件后.configure(services)和放置connect-history-api-fallback如上所示:

 app.use(compress()) .options('*', cors()) .use(cors()) // favicon and serveStatic used to be here .use(bodyParser.json()) .use(bodyParser.urlencoded({ extended: true })) .configure(hooks()) .configure(rest()) .configure(socketio()) .configure(services) .use(require('connect-history-api-fallback')()) // now they are down here .use(favicon(path.join(app.get('public'), 'favicon.ico'))) .use('/', serveStatic(app.get('public'))) .configure(middleware); 

我的问题是以下几点:这种方法有什么性能或安全方面的缺陷吗?

有了你发布的configuration,没有任何安全问题。

如果要用SSR中间件replaceserveStatic中间件,则需要在SSR中间件之前删除CORS头,以避免通过跨站点请求(使用Cookie的CSRF“攻击”)泄漏数据。

Interesting Posts