我将创建一个包罗万象的处理程序,该处理程序 在 发送必要数据的常规路由 之后 运行。
app = express();// your normal configuration like `app.use(express.bodyParser());` here// ...app.use(app.router);app.use(function(req, res) { // Use res.sendfile, as it streams instead of reading the file into memory. res.sendfile(__dirname + '/public/index.html');});app.router是运行您所有Express路由(例如
app.get和
app.post)的中间件;通常,Express会自动将其自动放置在中间件链的最末端,但您也可以像在此所做的那样将其显式添加到中间件链中。
然后,如果URL不是由处理的
app.router,则最后一个中间件将把Angular HTML视图发送给客户端。对于其他中间件未处理的 任何
URL,都会发生这种情况,因此您的Angular应用将必须正确处理无效路由。



