Node / Angular app Uncaught SyntaxError:意外的令牌< [英] Node / Angular app Uncaught SyntaxError: Unexpected token <

查看:151
本文介绍了Node / Angular app Uncaught SyntaxError:意外的令牌<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注此节点/ angular tutorial 并收到以下错误:

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

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

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');
        ...
    });

呈现:

<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>

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

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.

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

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

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.

编辑:

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

它应该能够找到凉亭组件中的东西,因为路径是正确的:

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


推荐答案

您在服务器上的app文件中缺少一些设置来处理所有正在发出的请求到服务器。这是一个基本的示例工作快速文件,您可以根据自己的环境进行修改:

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:

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 });
});

此文件使用 express.static 来提供服务无论名称或类型如何,特定目录中的任何内容。另请注意,Express use语句按顺序处理,并且第一次匹配,第一次匹配后的任何匹配都将被忽略。所以在这个例子中,如果它不是 / js / bower_components 中的文件, / css / partials 目录, index.html 将被退回。

This file uses express.static to 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 /partials directory, the index.html will be returned.

这篇关于Node / Angular app Uncaught SyntaxError:意外的令牌&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆