在我的节点js app.js中,我希望无论url是什么,它都会进入我的angular/ app.js页面,即begin.html,app.js驻留在服务器文件夹中,begin.html驻留在我的项目的客户端文件夹中。就像这样:-
-Project
----Server
---------app.js
----Client
---------begin.html
我应该在app.js中输入什么才能使所有urls都转到我使用Angular路由的begin.html??我想大概是这样的..。
var begin=require('../Client/begin.html);
app.use('*',begin);
发布于 2017-07-11 01:32:47
如果您打算让所有路由都返回到该HTML页面,那么您可以只使用Nginx这样的web服务器静态地为该目录提供服务。
看起来您正在使用Express,但这只是一个猜测。如果你想发出从Angular端到HTML端的Node.js请求,那么你可能希望默认响应返回你的HTML端,但仍然允许请求通过。我会考虑使用express中的static
方法来公开静态目录,同时仍然允许您构建其他路由(即api路由)。
它可能看起来像这样:
// Already created express app above
/*
This will default to using index.html from
your Client directory and serve any other resources
in that directory.
*/
app.use(express.static('../Client'));
// Any other routes
app.listen(3000); // Or whatever your port is
或者,您也可以使用404错误处理程序样式实现它,并返回默认文件:
/*
This comes after all of your other
route and middleware declarations and
before the .listen() call
*/
app.use(function(req, res, next) {
res.sendFile(path.join(__dirname + '/../begin.html'));
});
https://stackoverflow.com/questions/45018138
复制相似问题