javascript Node / Angular 应用程序未捕获语法错误:意外令牌 <

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31495743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:54:31  来源:igfitidea点击:

Node / Angular app Uncaught SyntaxError: Unexpected token <

javascriptangularjsnode.js

提问by Growler

I'm following this node/angular tutorialand am getting the following errors:

我正在关注此节点/角度教程,但出现以下错误:

enter image description here

在此处输入图片说明

I'm bootstrapping my app through node, which renders index page:

我正在通过节点引导我的应用程序,它呈现索引页面:

module.exports = function(app) {

    app.get('*', function(req, res) {
        res.sendfile('./public/index.html');
        ...
    });

Which renders:

其中呈现:

<html ng-app="DDE">
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>

    <script src="js/app.js"></script>
    <script src="js/controllers/main.js"></script>

</head>
<body>
    This is the index page
    <div ng-view></div>

I'd like Node to handle the initial page load, but Angular to handle the rest of the routing. The problem is here: It doesn't seem my angular routing is working. I put a self-exec fn run()in there to test, but it's not being called.

我希望 Node 处理初始页面加载,但 Angular 处理其余的路由。问题在这里:我的角度路由似乎不起作用。我run()在那里放了一个 self-exec fn来测试,但它没有被调用。

I'm simply trying to test display the testpage.htmltemplate:

我只是想测试显示testpage.html模板:

app.js file:

app.js 文件:

angular
    .module('DDE', [
        'ngRoute'
    ])
    .config(['$routeProvider',
        function($routeProvider) {
            $routeProvider.
                when('/test', {
                    run : (function() {
                        alert('hit');
                    })(),
                    templateUrl: '../html/partials/testpage.html'
                }).
                otherwise({
                    redirectTo: '/test'
                });
        }
    ]);

The angular error isn't very helpful. I'm not sure what Unexpected token <means as I cannot find where I've added an extra <anywhere.

角度误差不是很有帮助。我不确定是什么Unexpected token <意思,因为我找不到在<任何地方添加了额外内容的地方。



EDIT:

编辑:

app.get('/', function(req, res) {
    res.send('./public/index.html'); 
});

enter image description here

在此处输入图片说明

It should be able to find the stuff in bower components as the pathing is correct:

由于路径正确,它应该能够在凉亭组件中找到内容:

root/bower_components/angular/angular.js
root/bower_components/angular-route/angular-route.js

回答by Claies

You are missing some settings in your app file on the server to handle all of the requests being made to the server. Here is a basic sample working express file, which you can modify to fit your environment:

您在服务器上的应用程序文件中缺少一些设置来处理向服务器发出的所有请求。这是一个基本的示例工作 express 文件,您可以对其进行修改以适合您的环境:

var express = require('express');
var app = express();

app.use('/js', express.static(__dirname + '/js'));
app.use('/bower_components', express.static(__dirname + '/../bower_components'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/partials', express.static(__dirname + '/partials'));

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

This file uses express.staticto serve any content which is in the specific directory regardless of name or type. Also keep in mind that Express use statements are processed in order, and the first match is taken, any matches after the first are ignored. So in this example, if it isn't a file in the /js, /bower_components, /css, or /partialsdirectory, the index.htmlwill be returned.

此文件用于express.static提供特定目录中的任何内容,无论名称或类型如何。还要记住,Express use 语句是按顺序处理的,并采用第一个匹配项,第一个匹配项之后的任何匹配项都将被忽略。因此,在这个例子中,如果不是在一个文件中/js/bower_components/css,或/partials目录,index.html将被退回。

回答by mattyc

I was able to resolve my issue with the unexpected token by moving my server.js file from the DIST directory into the directory above.

通过将我的 server.js 文件从 DIST 目录移动到上面的目录,我能够解决意外令牌的问题。

So my folder/file structure looks like this:

所以我的文件夹/文件结构如下所示:

  • webroot - root folder

    • server.js

    • dist (folder)

      • browser (subfolder)

      • server (subfolder)

  • webroot - 根文件夹

    • 服务器.js

    • dist(文件夹)

      • 浏览器(子文件夹)

      • 服务器(子文件夹)