Node.js Express server(平均堆栈)在应用程序中使用通配符后永久停止工作。请使用(“*”)
我正在使用WebStorm作为IDE开发Ubuntu18和Ubuntu20。构建一个平均堆栈应用程序。 在使用Express服务器时,我遇到了一个我真的不理解的错误。我在网上搜索了几个小时的文档和其他答案,但没有找到任何对我有帮助的 这是应用程序的结构(仅显示相关文件): 我正在构建一个卑鄙的应用程序。服务器只需在任何路由上为预构建的静态Angular应用程序提供服务,但与Node.js Express server(平均堆栈)在应用程序中使用通配符后永久停止工作。请使用(“*”),node.js,angular,express,mean-stack,mean,Node.js,Angular,Express,Mean Stack,Mean,我正在使用WebStorm作为IDE开发Ubuntu18和Ubuntu20。构建一个平均堆栈应用程序。 在使用Express服务器时,我遇到了一个我真的不理解的错误。我在网上搜索了几个小时的文档和其他答案,但没有找到任何对我有帮助的 这是应用程序的结构(仅显示相关文件): 我正在构建一个卑鄙的应用程序。服务器只需在任何路由上为预构建的静态Angular应用程序提供服务,但与/api/* // server/server.js const app = express(); app.use
/api/*
// server/server.js
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(
session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: mongoose.connection })
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Routes
const staticAngularUI = path.join(__dirname, "../dist/coso/");
app.use("/", express.static(staticAngularUI));
app.use("/api/auth", auth);
app.get("/test", (req, res, next) => {
res.send('working');
return next();
});
到目前为止,该应用程序或多或少都能正常工作,它在root(/
)上提供角度用户界面。然而,此时,服务器将只在根(/
)上提供角度UI。这意味着在刷新时,如果我在任何其他路径上,它只会返回一个404
所以我做的改变打破了一切。我改变了这一行:
app.use("/", express.static(staticAngularUI));
到
从现在开始,当我请求任何根时,我得到:
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
最糟糕的部分和我真的不明白的部分来了:即使我回到了
app.use("/", express.static(staticAngularUI));
应用程序总是给我同样的错误(这怎么可能呢?!):
现在有趣的是,我没有得到一个错误,我得到了5个。为什么?我通过尝试不同的解决方案来修复服务器,找到了可能的答案:如果不是使用express.static()
I而是sendFile(path/to/index.html)
像这样:
app.use(express.static(staticAngularUI));
app.get("*", (req, res) => res.sendFile(path.join(staticAngularUI, 'index.html')));
我得到了不同的错误:
Uncaught SyntaxError: Unexpected token '<' http://…t:5000/runtime.js:1
Uncaught SyntaxError: Unexpected token '<' http://…5000/polyfills.js:1
Uncaught SyntaxError: Unexpected token '<' http://…st:5000/styles.js:1
Uncaught SyntaxError: Unexpected token '<' http://…st:5000/vendor.js:1
Uncaught SyntaxError: Unexpected token '<' http://…host:5000/main.js:1
未捕获的语法错误:意外标记'
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
Failed to load resource: the server responded with a status of 404 (Not Found) index.html
app.use(express.static(staticAngularUI));
app.get("*", (req, res) => res.sendFile(path.join(staticAngularUI, 'index.html')));
Uncaught SyntaxError: Unexpected token '<' http://…t:5000/runtime.js:1
Uncaught SyntaxError: Unexpected token '<' http://…5000/polyfills.js:1
Uncaught SyntaxError: Unexpected token '<' http://…st:5000/styles.js:1
Uncaught SyntaxError: Unexpected token '<' http://…st:5000/vendor.js:1
Uncaught SyntaxError: Unexpected token '<' http://…host:5000/main.js:1