在我的节点js app.js中,我希望无论网址是什么,它都会转到我的angular / html页面,即begin.html,app.js位于服务器文件夹中,begin.html位于我项目的客户端文件夹中。这就像: -
-Project
----Server
---------app.js
----Client
---------begin.html
我应该在app.js中输入什么以便所有网址都转到begin.html我使用的是Angular路由?我觉得它有点像......
var begin=require('../Client/begin.html);
app.use('*',begin);
答案 0 :(得分:1)
如果您要将所有路由返回到该HTML页面,那么您可以使用Nginx之类的Web服务器静态地为该目录提供服务。
看起来你正在使用Express,但这只是猜测。如果您想从Angular端向Node.js端发出HTTP请求,那么您可能希望默认响应返回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'));
});