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

我试图在localhost:9000的初始加载时显示home.html,但是当我执行当前代码的方式时,出现错误: friends.js:1 Uncaught SyntaxError: Unexpected token < 我不确定这个错误是什么意思。 此外,当我在display.js中执行windows.location = localhost:9000 / list时,get请求不会将list.html发送到浏览器,并且没有任何变化。 我尝试将get请求放入server.js和display.js中,但是它们都不执行任何操作。

目录布局

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

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

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

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

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

您尚未提供home.html文件,但是遇到的问题是Express找不到您的friends.js ,而是返回了home.html文件。 home.html文件中的第一个字符是< ,这就是为什么会出现错误的原因。 检查您引用的是friends.js的正确路径,并且该路径是否存在于静态资产文件夹中。

为了证实我的解释,您可以直接访问用于访问friends.js的URL,并查看返回的home.html的内容。

暂无
暂无

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

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