gpt4 book ai didi

node.js - 获取错误 "Unexpected token <"304 notmodified withexpress for vue.js 库

转载 作者:太空宇宙 更新时间:2023-11-04 00:11:29 25 4
gpt4 key购买 nike

Express服务器设置如下:

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


var app = express();

app.use(express.static('public'));


app.use('/', function (req, res) {
res.sendFile(path.resolve('public/index.html'));
});

var port = 3000;

app.listen(port, function(error) {
if (error) throw error;
console.log("Express server listening on port", port);
});

文件夹结构:

root folder
dist --- Vue.js
public --- index.html
src --- vuejsfiles.js
server.js
package.json
node-modules

index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<p> This is a vue </p>

<script src="../dist/vue.js" > </script>
<script src="../src/vueJS/vuejsfiles.js"> </script>
</body>
</html>

在使用 Unexpected token < 时,我在 chrome 开发控制台中收到错误“Vue.js ”缩小文件和 vuejsfiles.js 。控制台中的错误指向 <!DOCTYPE html>index.html文件和脚本没有被解析。仅当服务器正在运行时,此问题才会存在,否则如果在没有服务器的浏览器中打开同一文件,则会解析文件。由于我想使用服务器,如何解决这个问题?

最佳答案

您必须以某种方式公开这些文件夹,就像您对 static/ 所做的那样:

app.use(express.static('public'));
app.use('/dist', express.static('dist')); // added this
app.use('/src/vueJS', express.static('src')); // added this

// remove the lines below, public is already being served
// app.use('/', function (req, res) {
// res.sendFile(path.resolve('public/index.html'));
// });

并在 HTML 中不使用 ../ 来使用它:

<script src="dist/vue.js" > </script>
<script src="src/vueJS/vuejsfiles.js"> </script>

当然,您可以使用其他路径来代替 /dist/src/vueJS,只需记住也要更新 HTML 文件上的路径。

关于node.js - 获取错误 "Unexpected token <"304 notmodified withexpress for vue.js 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49306064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com