te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Express: Is it necessary to respond with status 200? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Express: Is it necessary to respond with status 200? - Stack Overflow

programmeradmin3浏览0评论

Is it necessary to respond with a status 200 code or is it the default behavior?

response.json({
  status: 'OK',
});

vs.

response
  .status(200)
  .json({
    status: 'OK',
  });

When I hit the route in my browser, I get a 200 response in both cases

By now, I only use status code for other responses than 200 (e.g. 404, 500)

Is it necessary to respond with a status 200 code or is it the default behavior?

response.json({
  status: 'OK',
});

vs.

response
  .status(200)
  .json({
    status: 'OK',
  });

When I hit the route in my browser, I get a 200 response in both cases

By now, I only use status code for other responses than 200 (e.g. 404, 500)

Share Improve this question edited Dec 26, 2021 at 10:00 marcobiedermann asked Nov 20, 2018 at 23:18 marcobiedermannmarcobiedermann 4,9235 gold badges28 silver badges39 bronze badges 5
  • I wouldn't say it is necessary, but it certainly is the convention. – Narm Commented Nov 20, 2018 at 23:22
  • No, it is not necessary. The former does so without calling .status(200). Why ask if you've already confirmed this? – Kevin B Commented Nov 20, 2018 at 23:26
  • 2 I'd say it's more of a best practice kinda thing... Personally... But no, it's not pulsory by any means. – JO3-W3B-D3V Commented Nov 20, 2018 at 23:29
  • @KevinB Thank you for your answer. I was asking because I was not sure about my statement and could not find any answer in the express documentation nor somewhere else. Everyone is using it differently and I was looking for a best practice and even if there might be still some benefit from one over the other – marcobiedermann Commented Nov 20, 2018 at 23:30
  • it's not at all a "best practice"... it's just a method you can call if you so choose to. it isn't necessary, you can accept the default, or pass a value to ensure it is what you want it to be. – Kevin B Commented Nov 20, 2018 at 23:34
Add a ment  | 

1 Answer 1

Reset to default 16

The Express response object wraps the underlying Node.js response object. In Node.js, if you don't set a response, it will always be 200. Express operates the same way for most requests. It will also automatically handle setting some error response codes for you depending on if and where an error was thrown.

Further, Express will set the response code for you on certain types of routes, for example, if you've defined a redirect, it will automatically set the 302 code for you.

发布评论

评论列表(0)

  1. 暂无评论