繁体   English   中英

在Express.js中使用Angular 1.5 Component路由器

[英]Using Angular 1.5 Component router with Express.js

我正在开发Express应用程序,我想捕获所有路由并将用户重定向到/public/app/index.html

app.all('/*', function (req, res, next) {
    // Just send the index.html for other files to support HTML5Mode
    res.sendFile('/public/app/index.html', { root: __dirname });
});

但是当我运行服务器时,这会显示在屏幕上:

================================================== -->

在控制台上出现以下错误:

jquery.min.js:1 Uncaught SyntaxError: Unexpected token <
hover.zoom.js:1 Uncaught SyntaxError: Unexpected token <
hover.zoom.conf.js:1 Uncaught SyntaxError: Unexpected token <
allJS.min.js:1 Uncaught SyntaxError: Unexpected token <

任何想法?

相关: SyntaxError:期望的表达式,得到了“ <”

发生的事情是您为应用程序发出的每个GET请求都获取了index.html

您可以通过检查routes文件中是否请求文件名来解决此问题。

注意:在此示例。 我使用app-root-path模块获取特定文件的路径

var appRoot = require('app-root-path');
...
app.get('/*', function(req, res) {
    var filename = req.originalUrl;
    //if it has a dot in its name return the original file
    if (/(\.)/g.test(req.originalUrl)){
        res.sendfile(appRoot + '/public/' + filename);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM