logo

该视频仅会员有权观看

立即开通课程「Node 框架 Express 实践」权限。

¥
199
/ 年

定义评论回复列表接口

Now we can define the comment reply board interface by defining the required handler in the comment controller. First, we import the previously defined get comment replies function from comment service, which retrieves a list of replies to a specific comment from the application's data repository. We name the handler for this interface 'index replies'.

Inside the index replies handler, we extract the comment ID from the URL parameters found in the request params object, then fetch the list of comment replies. We use a try-catch block where, in the try section, we declare a variable 'replies' as the comment replies by executing the get comment replies function. We parse the extracted comment ID into a decimal number and add a property 'crossid' to be processed.

We then respond to the client using the response's send method to send the obtained replies. In case of errors, we handle them in the catch block by executing next with the error, which will be dealt with by the application's error handler.

Finally, we open the comment router and define the reply list interface using the router's get method. The address for the interface is set as '/comment/commentid/replies', and the handler is set to 'index replies' from the comment controller.