作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 Angular2 开发 NodeJS 应用程序。在我的应用程序中,我有一个主页和搜索页面。对于主页,我有一个 HTML 页面,它将为 localhost:3000/ 和从主页用户导航到 search 即 localhost:3000/search angular2 routes 处理的 strong> 页面。
我没有搜索页面的页面,它的 View 由 angular2 呈现。但是当我直接点击 localhost:3000/search 因为我的 Node 应用程序中没有此路由时,它会给出错误。
我不知道如何在 Node 应用程序中处理这个?
最佳答案
如果您直接在浏览器导航栏中输入 localhost:3000/search
,您的浏览器会向您的服务器发出“/search”请求,在控制台中可以看到该请求(确保您检查“保留日志”按钮)。
Navigated to http://localhost:3000/search
如果您运行完全静态的服务器,则会产生错误,因为服务器上不存在搜索页面。例如,使用 express,您可以捕获这些请求并返回 index.html 文件。 angular2 bootstrap 启动,@RouteConfig 中描述的/search 路由被激活。
// 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 - 如何在 Nodejs 中处理 angular2 路由路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847972/
我是一名优秀的程序员,十分优秀!