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 - nodejs without stopping - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - nodejs without stopping - Stack Overflow

programmeradmin2浏览0评论

Is there a way I can make nodejs reload everytime it serves a page?

I want to do this during the dev cycle so I can avoid having to shutdown & startup on each code change?

Is there a way I can make nodejs reload everytime it serves a page?

I want to do this during the dev cycle so I can avoid having to shutdown & startup on each code change?

Share Improve this question edited Feb 26, 2012 at 19:59 Toby Allen 11.2k12 gold badges79 silver badges131 bronze badges asked Aug 1, 2010 at 16:31 Tyler GilliesTyler Gillies 1,9074 gold badges23 silver badges31 bronze badges 3
  • It seems you mean to hot reload edited source code during development cycle and not reload the whole process. – nalply Commented Aug 12, 2010 at 10:29
  • Does this do what you're looking for? phpasptutorialize.wordpress./2010/09/14/… – Garrett Serack Commented Dec 9, 2010 at 18:32
  • While this may not answer your question, it does explain the core concept of Node.js and does provide the module you need: devthought./2012/01/29/staying-up-with-node-js – Sơn Trần-Nguyễn Commented Feb 27, 2012 at 1:09
Add a ment  | 

5 Answers 5

Reset to default 9

Edit: Try nodules and their require.reloadable() function.

My former answer was about why not to reload the process of Node.js and does not really apply here. But I think it is still important, so I leave it here.

Node.js is evented IO and crafted specifically to avoid multiple threads or processes. The famous C10k problem asks how to serve 10 thousand clients simultaneously. This is where threads don't work very well. Node.js can serve 10 thousand clients with only one thread. If you were to restart Node.js each time you would severely cripple Node.js.

What does evented IO mean?

To take your example: serving a page. Each time Node.js is about to serve a page, a callback is called by the event loop. The event loop is inherent to each Node.js application and starts running after initializations have pleted. Node.js on the server-side works exactly like client-side Javascript in the browser. Whenever an event (mouse-click, timeout, etc.) happens, a callback - an event handler - is called.

And on the server side? Let's have a look at a simple HTTP server (source code example taken from Node.js documentation)

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

This first loads the http module, then creates an HTTP server and tells it to invoke the inner function starting with function (request, response) every time an HTTP request es in, then makes the server listen to port 8124. This pletes almost immediately so that console.log will be executed thereafter.

Now Node.js event loop takes over. The application does not end but waits for requests. And voilà each request is answered with Hello World\n.

In a summary, don't restart Node.js, but let its event loop decide when your code has to be run.

Found Nodemon, exactly what I wanted: https://github./remy/nodemon

I personnaly use spark2 (the fork) and will switch to cluster as soon as i found the time to test it. Among other things, those 2 will listen to file changes and reload the server when appropriate, which seems to be what you're looking for.

There are many apps for doing this, but I think the best is Cluster, since you have 0 downtime for your server. You can also set multiple workers with it or manually start/stop/restart/show stats with the cli REPL functionality.

No. Node.js should always run. I think you may have misunderstood the concept of nodejs. In order for nodejs to serve pages, it has to be its own server. Once you start a server instance and start listening on ports, it will run until you close it.

Is it possible that you've called a function to close the server instead of just closing a given stream?

发布评论

评论列表(0)

  1. 暂无评论