如何处理Nodejs中的angular2路由路径? [英] How to handle angular2 route path in Nodejs?

查看:33
本文介绍了如何处理Nodejs中的angular2路由路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Angular2 开发 NodeJS 应用程序.在我的应用程序中,我有一个主页和搜索页面.对于主页,我有一个 HTML 页面,它将为 localhost:3000/ 呈现,并从主页用户导航到 searchlocalhost:3000/searchangular2 路由 处理的strong> 页面.

I am working on a NodeJS app with Angular2. In my app, I have a home page and search page. For home page I have an HTML page that will render for the localhost:3000/ and from home page user navigate to search i.e localhost:3000/search page that I handled by angular2 routes.

我没有搜索页面的页面,它的视图由 angular2 呈现.但是当我直接点击 localhost:3000/search 时,因为我的节点应用程序中没有这个路由,它给出了错误.

I don't have the page for the search page its view render by the angular2. But when I directly hit localhost:3000/search as I don't have this routing in my node app it gives the error.

我不知道如何在节点应用程序中处理这个问题?

I don't know How to handle this in node app?

推荐答案

如果在浏览器导航栏中直接输入 localhost:3000/search ,浏览器会向 '/search' 发出请求您的服务器,可以在控制台中看到(确保选中保留日志"按钮).

If you enter localhost:3000/search directly in the browser navigation bar, your browser issues an request to '/search' to your server, which can be seen in the console (make sure you check the 'Preserve Log' button).

Navigated to http://localhost:3000/search

如果您运行完全静态的服务器,则会产生错误,因为服务器上不存在搜索页面.例如,使用 express,您可以捕获这些请求并返回 index.html 文件.angular2 引导程序启动,@RouteConfig 中描述的/search 路由被激活.

If you run a fully static server, this generates an error, as the search page does not exist on the server. Using express, for example, you can catch these requests and returns the index.html file. The angular2 bootstrap kicks-in, and the /search route described in your @RouteConfig is activated.

// example of express()
let app = express();
app.use(express.static(static_dir));

// Additional web services goes here
...

// 404 catch 
app.all('*', (req: any, res: any) => {
  console.log(`[TRACE] Server 404 request: ${req.originalUrl}`);
  res.status(200).sendFile(index_file);
});

这篇关于如何处理Nodejs中的angular2路由路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆