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

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

问题描述

我正在使用Angular2开发NodeJS应用程序.在我的应用程序中,我有一个主页和搜索页面.对于主页,我有一个HTML页面,该页面将为 localhost:3000/呈现,并从主页用户导航至 search ,即 localhost:3000/search angular2路线处理的"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,浏览器将向服务器发出"/搜索"请求,该请求可以在控制台(请确保选中保留日志"按钮).

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);
});

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

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