mongodb 无法加载模块脚本:应为javascript模块脚本,但服务器响应的MIME类型为text/jsx

rqdpfwrv  于 2022-12-12  发布在  Go
关注(0)|答案(1)|浏览(188)

我创建了一个快递服务器,并创建了一个API,也我安装了React通过vite为我的前端.当我试图连接或加载我的主要html文件到服务器的控制台给出了错误.我新的快递
这是我的快捷应用程序代码

const express = require("express");
const dotenv = require("dotenv");
const errorMiddleware = require("./middleware/error");
const cookieParser = require("cookie-parser");
const fileupload = require("express-fileupload");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const path = require("path");
//config for  env file
dotenv.config({ path: `${__dirname}/config/config.env` });

app.use(cors());
app.use(express.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb" }));
app.use(cookieParser());
app.use(fileupload());
// Route Imports
const productRoutes = require("./routes/productRoutes");
const userRoutes = require("./routes/userRoutes");
const orderRoutes = require("./routes/orderRoute");
const paymentRoute = require("./routes/paymentRoute");

//using all routes
app.use("/api/v1", productRoutes);
app.use("/api/v1/user", userRoutes);
app.use("/api/v1", orderRoutes);
app.use("/api/v1", paymentRoute);

app.use(express.static(path.join(__dirname, "../frontend")));
app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "../frontend/index.html"));
});
//error HAndler Middleware
app.use(errorMiddleware);
module.exports = app;
xnifntxz

xnifntxz1#

您不能提供您的JSX文件,您需要首先构建您的前端,因此npm run build,然后提供您的构建文件夹的内容(在我的例子中是dist/

app.use(express.static(path.join(__dirname, "./front/dist/")));
app.get("*", (req, res) => {
  res.sendFile(path.resolve(__dirname, "./front/dist/index.html"));
})

相关问题