简体   繁体   English

Uncaught SyntaxError: Unexpected token '<' - Node.js 托管的 HTML 中的 JavaScript 文件引用立即出错

[英]Uncaught SyntaxError: Unexpected token '<' - Error immedately upon JavaScript file reference in HTML hosted by Node.js

I am using Node in order to run my website that uses HTML and JavaScript. I am getting this error upon running my code: Uncaught SyntaxError: Unexpected token '<'我正在使用 Node 来运行我的网站,该网站使用 HTML 和 JavaScript。运行我的代码时出现此错误: Uncaught SyntaxError: Unexpected token '<'

I suspect that a possible issue might be the way I am importing/exporting the SQL connection?我怀疑可能的问题可能是我导入/导出 SQL 连接的方式? I have tried reworking the way I do the import and export in order to possibly fix the issue but no dice so far.我已经尝试修改导入和导出的方式,以便可能解决问题,但到目前为止还没有成功。

Here is my code:这是我的代码:

Node.js File "server.js" Node.js 文件“server.js”

let http = require('http');
let fs = require('fs');

const mysql = require('mysql');

let con = mysql.createConnection({
    host: "***",
    user: "***",
    password: "***",
    database: "***",
    charset: "utf8"
});

module.exports = { con }

let handleRequest = (request, response) => {
    response.writeHead(200, {
        'Content-Type': 'text/html'
    });
    fs.readFile('./main.html', null, function(error, data) {
        if (error) {
            response.writeHead(404);
            response.write('File Not Found');
        } else {
            response.write(data);
        }
        response.end();
    });
};

http.createServer(handleRequest).listen(8000);

JavaScript File "reference.js JavaScript 文件“reference.js

import {con} from 'server.js';

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        console.log("Geolocation not supported!");
    }
}


let longitude = document.getElementById("longitude");
let latitude = document.getElementById("latitude");

function showPosition(position) {

    longitude.value = position.coords.longitude;
    latitude.value = position.coords.latitude;
}

function initMap() {
    const myLoc = {lat: 0, lng: 0};

    const map = new google.maps.Map(document.getElementById("map"), {
        zoom: 14,
        center: myLoc,
        map: myLoc,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    const marker = new google.maps.Marker({
        position: myLoc,
        map: map
    });
}

function addLocation() {
    let newLat = document.getElementById("latitude").value;
    let newLng = document.getElementById("longitude").value;
    let id = document.getElementById("id").value;

    let statement = "INSERT INTO data(id, longitude, latitude) VALUES (?,?,?)";
    let entry = [id, newLng, newLat];

    // id, longitude, latitude
    con.query(statement, entry, (err, results, fields) => {
        if (err) {
            return console.error(err.message);
        }

        console.log("Entry ID: " + results.insertId);
    });
}

function displayLocations() {

    let statement = "";

    con.query(statement, (err, results, fields) => {
        if (err) {
            return console.error(err.message);
        }

        document.getElementById("locations").innerText = results;
    });
}

HTML File contains this line in the head: <script type="text/javascript" src="./reference.js"></script> HTML 文件的头部包含这一行: <script type="text/javascript" src="./reference.js"></script>

From what I understand by reading your code, you're doing a few things in the wrong way.根据我通过阅读您的代码所了解的情况,您以错误的方式做了一些事情。

First, your server.js file should be run by Node.js, and your reference.js should be run by the browser.首先,你的 server.js 文件应该由 Node.js 运行,你的 reference.js 应该由浏览器运行。 You can't just import your server.js file directly into your reference.js file since they both are supposed to run in different environments.您不能直接将 server.js 文件导入到 reference.js 文件中,因为它们都应该在不同的环境中运行。

Instead, you should make a request from your frontend(application running on browser) to the backend (application running on NodeJS).相反,您应该从前端(在浏览器上运行的应用程序)向后端(在 NodeJS 上运行的应用程序)发出请求。

Please, look for how to make requests from javascript frontend application to a backend server application.请查看如何从 javascript 前端应用程序向后端服务器应用程序发出请求。 There are a lot of good tutorials that you can learn from.有很多很好的教程,您可以从中学习。

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

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