简体   繁体   English

错误:friends.js:1未捕获的语法错误:使用express.static后出现意外的令牌<,并在单击按钮后更改html内容

[英]Error: friends.js:1 Uncaught SyntaxError: Unexpected token <, after using express.static and change html content after button click

I am trying to display home.html on initial load of localhost:9000, but when I do when the way my current code is, I get the error: friends.js:1 Uncaught SyntaxError: Unexpected token < . 我试图在localhost:9000的初始加载时显示home.html,但是当我执行当前代码的方式时,出现错误: friends.js:1 Uncaught SyntaxError: Unexpected token < I am not sure what this error means. 我不确定这个错误是什么意思。 Also, when I do windows.location = localhost:9000/list, in my display.js, the get requests will not send the list.html to the browser, and nothing changes. 此外,当我在display.js中执行windows.location = localhost:9000 / list时,get请求不会将list.html发送到浏览器,并且没有任何变化。 I tried putting the get request in both server.js and display.js but they both do nothing. 我尝试将get请求放入server.js和display.js中,但是它们都不执行任何操作。

Directory layout 目录布局

dir main
    -server.js
    dir subMain
      dir display
        -display.js
      dir routing
        -routes.js
      dir public
        -home.html
        -list.html

server.js server.js

var path = require('path');
var express = require('express');
var app = express();
require('./subMain/routing/routes.js')(app, path, express);

app.listen(9000, function(){
     console.log('connected on 9000')
})



//app.get('/list', function(request, response){
       // response.sendFile(path.join(__dirname + '/..', 'public', 'list.html'));
    //});

routes.js routes.js

module.exports = function(app, path, express){
    app.use(express.static("subMain"))
    app.use(express.static(__dirname + "/public"));
    app.use(express.static(__dirname + "/routing"));
    app.use(express.static(__dirname + "/display"));
    app.use(function(request, response, next){
      response.sendFile(path.join(__dirname + "/..", "public", "home.html"));
    })
    app.get('/list', function(request, response){
        response.sendFile(path.join(__dirname + '/..', 'public', 'list.html'));
    });

}

display.js display.js

$(document).on('click', '#btn', sendSurvery);

function sendSurvery(){
    window.location = 'survey.html';
    //var myQueryUrl = "http://localhost:9000/survey";

    //$.ajax({url: myQueryUrl, method: 'GET', success: function(result){
        // location.href = "http://localhost:9000/list"

    //}}).done(function(response){

    //});
}

home.html home.html

<!DOCTYPE html>
<html>
<head>
    <title>Friend Finder Home Page</title>

</head>
<body>
    <div class="rowOne">
        <!-- <div class="jumbotron col-lg-6"> -->
          <h1>Hello, world!</h1>
          <p>Click the button</p>
          <button id="btn" style="width: 200px;">BUTTON</button>
        <!-- </div> -->
    </div>
<script src="https://code.jquery.com/jquery.js"></script>
<script type="text/javascript" src="../data/friends.js"></script>
</body>
</html> 

You haven't supplied your home.html file, but the issue you're encountering is that your friends.js is not being found by express, and your home.html file is being returned instead. 您尚未提供home.html文件,但是遇到的问题是Express找不到您的friends.js ,而是返回了home.html文件。 The first character in your home.html file is < and this is why you're getting the error. home.html文件中的第一个字符是< ,这就是为什么会出现错误的原因。 Check you are referencing the correct path of friends.js and that it is present in your static assets folder. 检查您引用的是friends.js的正确路径,并且该路径是否存在于静态资产文件夹中。

To confirm my explanation, you can directly access the url you're using to access friends.js and see the contents of home.html returned. 为了证实我的解释,您可以直接访问用于访问friends.js的URL,并查看返回的home.html的内容。

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

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