404错误JavaScript和CSS文件在Vue.js + Node.js应用程序上的Live Server中

huangapple go评论78阅读模式
英文:

404 Error for JavaScript and CSS Files in Vue.js + Node.js App on Live Server

问题

我有一个Vue.js + Node.js应用程序,在我的本地服务器上完美运行。然而,当我将它部署到一个实际服务器时,我遇到了JavaScript和CSS文件的404错误。我已经验证这些文件存在于实际服务器上的正确目录中,并且已经调整了index.html文件中的文件路径。尽管做了这些努力,我仍然无法访问这些文件。

以下是index.html中的相关代码:

client/dist/index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + Vue</title>
    <script type="module" crossorigin src="/assets/index-2b384caf.js"></script>
    <link rel="stylesheet" href="/assets/index-d1b70ef7.css">
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

server.js中的相关代码:

const express = require('express');
const cors = require('cors');
const routes = require('./routers');

const app = express();
const path = require('path');

// 从Vue.js客户端构建中提供静态文件
app.use(express.static(path.join(__dirname, '../client/dist')));

app.use(express.json());
app.use(cors());
app.use('/api', routes);

// 对于任何其他路由,提供Vue.js应用程序
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '../client/dist/index.html'));
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

我已经检查了以下内容:

  1. 文件存在于实际服务器上的正确目录中。
  2. index.html文件中的文件路径正确,考虑到服务器的目录结构。
  3. 服务器配置已设置为从客户端目录提供静态文件。
  4. 我已清除了浏览器缓存并尝试在隐身窗口中访问这些文件。

尽管进行了这些检查,但我仍然在尝试访问JavaScript和CSS文件时收到404错误。

目录结构:

项目目录

  1. client(Vue.js)
  2. server(Node.js)

对于如何排除和解决此问题的任何建议或想法都将不胜感激。谢谢!

英文:

I have a Vue.js + Node.js application that works perfectly on my local server. However, when I deploy it to a live server, I encounter a 404 error for the JavaScript and CSS files. I have verified that the files exist in the correct directories on the live server, and I've adjusted the file paths in the index.html file. Despite these efforts, I still cannot access the files.

Here's the relevant code in index.html:

client/dist/index.html

&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;link rel=&quot;icon&quot; type=&quot;image/svg+xml&quot; href=&quot;/vite.svg&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;title&gt;Vite + Vue&lt;/title&gt;
    &lt;script type=&quot;module&quot; crossorigin src=&quot;/assets/index-2b384caf.js&quot;&gt;&lt;/script&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;/assets/index-d1b70ef7.css&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

server/server.js

const express = require(&#39;express&#39;);
const cors = require(&#39;cors&#39;);
const routes = require(&#39;./routers&#39;);


const app = express();

const path = require(&#39;path&#39;);

// Serve static files from the Vue.js client build
app.use(express.static(path.join(__dirname, &#39;../client/dist&#39;)));

app.use(express.json());

app.use(cors());

app.use(&#39;/api&#39;, routes);

// For any other routes, serve the Vue.js app
app.get(&#39;*&#39;, (req, res) =&gt; {
  res.sendFile(path.join(__dirname, &#39;../client/dist/index.html&#39;));
});

const port = process.env.PORT || 3000;
app.listen(port, () =&gt; {
  console.log(`Server is running on port ${port}`);
});

I've already checked the following:

The files exist in the correct directories on the live server.
The file paths in the index.html file are correct, taking into account the server's directory structure.
The server configuration is set up to serve static files from the client directory.
I've cleared the browser cache and tried accessing the files in an incognito window.
Despite these checks, I still receive a 404 error when trying to access the JavaScript and CSS files.

directory structure

project directory

  1. client (vuejs)
  2. server (nodejs)

Any suggestions or ideas on how to troubleshoot and resolve this issue would be greatly appreciated. Thank you!

答案1

得分: 0

终于我成功修复了问题。实际上,我在/client/vite.config.js文件中配置错误。

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  plugins: [vue()],
  base: '/client/dist/' // 添加了这一行
});

添加了base: '/client/dist/'之后,它就像魔法般地工作了。

英文:

Finally I able to fix the issue. actaully I had wrong configuation on /client/vite.config.js

import { defineConfig } from &#39;vite&#39;;
import vue from &#39;@vitejs/plugin-vue&#39;;

export default defineConfig({
  plugins: [vue()],
  base: &#39;/client/dist/&#39; // added this line
});

after adding base: '/client/dist/' it works like a charm.

huangapple
  • 本文由 发表于 2023年7月20日 21:29:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定