Angular和NodeJS使用参数获取请求url只返回响应对象no组件HTML
我有一个像例如'localhost:3000 / verify / a5d5sd'的URL,我发送到用户的电子邮件,点击这个链接后,我在链接中使用param(a5d5sd)来检查服务器中的一些东西,数据库(MongoDB)并返回一个对象的响应,现在当链接被点击或打开一个新的标签时,我只能看到来自服务器的响应,而不是来自我的Angular 2组件的HTML。
在我的Angular 2路由configuration模块中,我configuration了一个类似的路由
{ path: 'verify/:id', component: MyComponent },
而在这个组件的ngOnInit,我打电话给一个服务方法,使GET HTTP请求与URL像validation/:ID 帕拉姆(:ID)我得到它使用@ angular /路由器的ActivatedRoute,使我可以请求。
为了解决这个问题,我被build议为我的快递使用以下代码:
app.use(express.static(path.join(__dirname, 'client/dist'))); app.get('*', function(req, res) { res.sendFile(__dirname + '/client/dist/index.html'); })
甚至尝试过:
app.all('*', function(req, res) { res.sendFile(__dirname + '/client/dist/index.html'); })
此代码适用于直接导航(在search栏上粘贴URL),并仅刷新POST和PUT请求,但不能与GET请求一起使用,这些请求需要服务器端的响应,当我直接访问URL或刷新页面时,DOM写与服务器响应,但不是HTML,任何帮助将不胜感激。
在定义所有路由之后,添加index.html文件的路由
// Set your routes here app.use('/api', api); // Catch all other routes and return the index file app.get('*', function(req, res) { res.sendFile(path.join(__dirname, 'path/to/index.html')); //path from the root dir });
这是为我工作。 谢谢