【发布时间】:2016-10-03 05:01:39
【问题描述】:
我正在快速服务器中构建一个 Vuejs 应用程序。 我想为所有路由显示我的应用程序,然后我将在我的 Vue 应用程序路由器中管理这些路由。
如果我定义了一个独特的路由,例如 /home,我的 Vue 应用程序会很好地显示,但是当我将路由器定义为 /* 以允许所有路由时,渲染不起作用
router.get('/*', function (req, res) {
return res.render("application")
});
app.js:1 Uncaught SyntaxError: Unexpected token
application.html
<!DOCTYPE html>
<html>
<head>
<title>My title</title>
</head>
<body>
<div id="app"></div>
<script src="public/app.js"></script>
</body>
</html>
错误是因为app.js 被读取为application.html 文件(奇怪)。但是,如果我定义了一条独特的路线(例如:/home),则app.js 的内容是正确的,并且我的应用程序可以正常工作。
以防万一:app.js
var express = require('express'),
app = express(),
router = express.Router(),
mustacheExpress = require('mustache-express')
app.use(router)
app.engine('html', mustacheExpress());
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use('/public', express.static('public'));
router.get('/*', function (req, res) {
return res.render("application");
});
app.listen(9000)
module.exports = app;
更新
正如@rsp 所说,问题来自我的html文件中包含的脚本(app.js),因为它也匹配/*路由
现在更新的新问题是:如何只为路由中的那些“公共”文件设置异常?
【问题讨论】:
-
该更新问题的解决方案在这里:stackoverflow.com/questions/21764155/…
标签: node.js express routes vue.js