具有node.js的Webworkers表示应用程序

我是node.js的新手,正在尝试构build一个快速应用程序。 到目前为止,我已经安装了一个应用程序,并创build了一些基本路线。

我的应用程序的入口点是一个文件,我正在使用应用程序设置服务器:

#!/usr/bin/env node /** * Module dependencies. */ var app = require('../app'); var debug = require('debug')('main'); var http = require('http'); /** * Get port from environment and store in Express. */ var port = normalizePort(process.env.PORT || '3000'); app.set('port', port); /** * Create HTTP server. */ var server = http.createServer(app); /** * Listen on provided port, on all network interfaces. */ server.listen(port); server.on('error', onError); server.on('listening', onListening); /** * Normalize a port into a number, string, or false. */ function normalizePort(val) { var port = parseInt(val, 10); if (isNaN(port)) { // named pipe return val; } if (port >= 0) { // port number return port; } return false; } /** * Event listener for HTTP server "error" event. */ function onError(error) { if (error.syscall !== 'listen') { throw error; } var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': console.error(bind + ' requires elevated privileges'); process.exit(1); break; case 'EADDRINUSE': console.error(bind + ' is already in use'); process.exit(1); break; default: throw error; } } /** * Event listener for HTTP server "listening" event. */ function onListening() { var addr = server.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); } 

快速应用程序使用mongoose在适当的http请求上与数据库进行通信。

在这一点上,我还需要一个networking工作者,当服务器启动时,每5秒生成一个随机数字,如果生成的数字是10,则将其识别为一个事件,并将一些结果存储在数据库中。

我不确定如何从这个开始,并将其纳入到目前的结构中。

我已经有接口设置写入数据库等我只是失去了networking工作者的实施和集成的一部分。

在node.js中没有webworkers。 但是我们有一些类似的叫做child_process (更多细节请看文档链接 )

如何使用child_process? 在stackoverflow中有很多的资料: https : //stackoverflow.com/a/13371439/4138339有关于主模块如何运行其他模块的说明(查看详情):在你的主模块中:

 var cp = require('child_process'); var child = cp.fork('./othermodulefile'); child.on('message', function(m) { // Receive results from child process console.log('received: ' + m); }); // Send child process some work child.send('Please up-case this string'); 

或者,请查看这个链接的网页http://www.tutorialspoint.com/nodejs/nodejs_scaling_application.htm,并从该页面展开: File:support.js

 console.log("Child Process " + process.argv[2] + " executed." ); 

文件:master.js

 const fs = require('fs'); const child_process = require('child_process'); for(var i=0; i<3; i++) { var workerProcess = child_process.exec('node support.js '+i, function (error, stdout, stderr) { if (error) { console.log(error.stack); console.log('Error code: '+error.code); console.log('Signal received: '+error.signal); } console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); }); workerProcess.on('exit', function (code) { console.log('Child process exited with exit code '+code); }); } 

使用child_process的一些缺点: https ://stackoverflow.com/a/17340465/4138339

每个线程都花费内存,这就是为什么大多数传统系统没有太多的可扩展性,因为每个客户端都需要线程。 对于节点来说,从硬件资源的angular度来看,效率会非常低。

和其他部分:

build议工作人员数量与硬件上的CPU内核数量相等。

其他的可能性,如果你需要运行一些基于时间的作业调度器看看这个模块https://github.com/ncb000gt/node-cron 。 这是node.js cron作业。 安装npm install cron和用法示例:

 var CronJob = require('cron').CronJob; new CronJob('* * * * * *', function() { console.log('You will see this message every second'); }, null, true, 'America/Los_Angeles');