w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
node.js & express - global modules & best practices for application structure
I found this StackOverflow post very helpful: File Structure of Mongoose & NodeJS Project The trick is to put your schema into models directory. Then, in any route, you can require('../models').whatever. Also, I generally start the mongoose db connection in app.js, and only start the Express server once the connection is up: mongoose.connect('mongodb://localhost/whateverdb') mongoose.connection.on('error', function(err) { console.log("Error while connecting to MongoDB: " + err); process.exit(); }); mongoose.connection.on('connected', function(err) { console.log('mongoose is now connected'); // start app here http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); });

Categories : Javascript

Astyanax requests fail in 4-node cluster when single seed node is down
The contact point provided always needs to be running when you issue a query with Astyanax. The learning kicks in afterwards when you start writing to multiple replicas and the driver needs to workout where to send extra replicated data (because as you know, if you have more than 1 node, data gets written to multiple partitions) What exactly do I mean? 127.0.0.1 <--- seed 127.0.0.2 127.0.0.3 127.0.0.4 // code where you initialize Astyanax ... .setSeeds("127.0.0.1") // this node always has to be available ...

Categories : Cassandra

Knowing which real node is up in a Cassandra cluster under virtual node setting
You should use nodetool status, which outputs just one line per node e.g. $ bin/nodetool status Datacenter: datacenter1 ======================= Status=Up/Down |/ State=Normal/Leaving/Joining/Moving -- Address Load Tokens Owns Host ID Rack UN 127.0.0.1 152.64 KB 256 100.0% 22f70e40-4070-483a-9fa6-e272556b7164 rack1

Categories : Cassandra

A list of modules or classes ( in VB 2010 Express)
In retrospect this might be simpler. You should be making them classes in my opinion not modules... Please note that I have used One, Two, and Three below to represent an object of your different types. Dim col as List(Of Object) col.add(One) col.add(Two) col.add(Three) For i as integer = 0 to col.Count -1 If typeof col(i) is [ModuleType1] Then CTYPE(col(i), ModuleType1).SubClass.Method() Else If TypeOf col(i) is ModuleType2 Then CTYPE(col(i), ModuleType2.SubClass2.Method2() End If Next Optionally, you can just make col an array of Objects. Dim col() as Object = {One, Two, Three} Ideally you would avoid using Object and instead all of your modules would use inheritance to group like methods together. They instead of Object you could use the ParentObject w

Categories : Vb.Net

Could not connect to a primary node for replica set ]>
First See if your database is running by mongo , If yes Use this command: sudo rm /var/lib/mongodb/mongod.lock mongod --repair sudo service mongodb start Your database will work.

Categories : Ruby On Rails

Node-sass is not auto-compiling in latest node/express
Middlewares are executed in the order they are attached to the app. The node-sass middleware only compiles scss files to css, it does not serve them. The static middleware is able to serve the compiled css files, however, it can’t do so as long as the css files are not compiled yet. If you switch static and sass middlewares, everything should work as expected: app.use( sass.middleware({ src: __dirname + '/public/sass', dest: __dirname + '/public', debug: true, outputStyle: 'compressed' }) ); app.use(express.static(path.join(__dirname, 'public'))); Now the following happens when you request /style.css: sass notices a request for a css file. If the dest file (__dirname + '/public' + '/style.css') is up-to-date, it does nothing. Otherwise, it looks for the src fil

Categories : Node Js

How do you mock MySQL (with node-orm2) in Node.js/Express?
If you want to mock the req using sinon.js, you can do something like the following. var sinon = require('sinon'); var friend = require('./friend'); it('some test', function(done) { var req = { currentUser: { // Add all the properties/functions that you are concerned with here. // and you can/should wrap them around sinon.spy(). // If you are not concerned with that function (i.e. you are not using it) // then you can simply use sinon.stub() to return a stub function. } }; var res = { send: sinon.spy(function(obj) { assert.ok(obj); // yes object exists done(); // end of test }; }; var next = function() {}; friend.findActiveFriend(req, res, next); }); This way you shouldn't be connecting to the model, which tests frien

Categories : Mysql

SocketIO on a Node.js cluster
There is no "simple" solution. What you have to do is the following: If a client connects to a worker, save the connection-id together with the worker-id and a potential additional identification-id in a global (=for all workers accessible) store (i.e. redis). If a client gets routed to another worker, use the store to look up which worker is reponsible for this client (either with the connection-id or with the additional identification-id and then hand it over to that worker (either with the nodejs-worker-master-worker-communication or via redis-pub-sub) I habe implemented such thing with sock.js and an additional degree of complexity: I have two node.js servers with four workers each, so I had to use redis-pub-sub for worker/worker communication, because it is not guaranteed that the

Categories : Node Js

Node.js Cluster not listening
I fixed the issue by running the file directly. Previously, I ran server.js which was a one liner: server.js require("./server/app.js"); server/app.js (code snippet in the question) By running node app.js instead of node server.js the cluster started listening. Not sure why this made a difference.

Categories : Node Js

Tornado or node? what about express? node-django?
Tornado it's a good choice but I do not like the doc another one is Twisted this one has a good documentation I like so much. I'm also a Django developer and I like so much Node.js this one has a big community a lot of documentation also has a very cool librarie for WebSockets socket.io about express It's the backend of MySpace but I do not like so much express. Combine Django and Node.js It's not so hard but it's a quite dangerous. I did a demo of how to combine them here you have the github

Categories : Django

Setting up a 2-node Hadoop cluster on VM
Looks like there is some problem with your cluster. Your web console is showing zero nodes. Make sure your Hadoop daemons are running fine. Were you able to start them properly? Use JPS to check if the daemons are running properly.

Categories : Linux

Node.js - child_process and cluster confusion
1) child_process.fork() returns the forked process in the same way as child_process.spawn() returns the newly spawned process. Indeed, fork() is just [...] a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.1 2) process in the child refers to the child process. Also, In the child the process object will have a send() method, and process will emit objects each time it receives a message on its channel.2 So from within the child we can send messaged with 'send()' and we can receive them with .on('message'). Like in your snippet. 3) As stated in the documentation about the cluster module on Node.js: The worker processes are sp

Categories : Javascript

Running EJB timer on each node in a cluster
Yes, you want a non-persistent timer. Per the EJB 3.1 specification: Non-persistent timers can be created programmatically or automatically (using @Schedule or the deployment descriptor). For automatic non-persistent timers, the container creates a new non-persistent timer during application initialization for each JVM across which the container is distributed. (As an aside, Derby databases can only be shared by multiple processes if you use Derby network server. Multiple application server processes cannot open a Derby database simultaneously.)

Categories : Java

having a global object with cluster module of node.js
All worker processes are indeed new copies of your application variables independently. Each worker is a full featured process created with child_process.fork. In documents you can get following statement: No shared state between the workers. Because workers are all separate processes, they can be killed or re-spawned depending on your program's needs, without affecting other workers So answer is no you cannot share a variable counter, Your cluster processes don't share variables among themselves.

Categories : Node Js

Node.js cluster and socket.io with mongodb as store
Any of clustered worker can easily access MongoDB using mongodb or mongoose in order to get required data. As well it can store that data locally as a cache. Once socket.io client is connected it will be bound to one of workers and will not swap workers during execution. In order to share sessions you need to create session middleware for socket.io that will ask for session details from MongoDB from one of the workers. Check this answer which explains how to access session data in socket.io from shared session details. And in order to make it clustered, instead of MemoryStorage use connect-mongo that allows to store session data within mongo and it will make session details shared over workers.

Categories : Node Js

Connecting to Cassandra Cluster instead of specific node
Yes. If you're using the Datastax Java driver, you can get all of these benefits and more. From the documentation: The driver has the following features: connection pooling node discovery automatic failover load balancing

Categories : Cassandra

Any additional considerations when using Faye in a Node.js cluster?
I just realized that the answer to my question is fairly obvious. One thing to be aware of is that Faye will need to access shared state across multiple server instances (processes). In a single-server config, you could probably get away with using Faye's memory engine. In a clustered config, you'd need to use Faye's redis engine or some other engine that allows state to be shared by different processes. I'd prefer not to introduce another persistence component just for this purpose so I may look into implementing my own on top of my current persistent store (Neo4j).

Categories : Misc

How to remove node from elasticsearch cluster on runtime without down time
You can always kill the process to remove the node at runtime. Elasticsearch cluster will automatically rebalance in the remaining nodes. But, there are some cons here. How many replicas do you have in your settings? How many shards do you have in your settings? If the data present in shards are too large(10GB Per Hour) then it might take long time to rebalance. If you have enough replicas then the cluster state would go to yellow. In this state you can index and search during this time. However the shards go to unassigned initially and then move to balance nodes slowly.

Categories : Elasticsearch

cluster:node; need a way to differentiate in master when to fork worker
you should use cluster.on('exit', function (worker, code, signal) because you can inspect the variable code and signal to determine what wrong and when to fork. cluster.on('exit', function (worker, code, signal) { logger.log.warn('Worker server died (ID: %d, PID: %d)', worker.id, worker.process.pid); cluster.fork(); });

Categories : Node Js

Recommendation on multi-node hadoop cluster installation
I am using Apache Hadoop not much issues except that I have to resolve any compatibility issue while using hadoop eco system components such as hive, pig, sqoop etc. Cloudera Manager on the other side take care of most of these compatibility issues and kind of provides u a complete package with support. Hope this helps!

Categories : Hadoop

Debugging stray uncaught exceptions (ECONNRESET) in a node cluster
This answer was helpful: http://stackoverflow.com/a/11542134/233370 Basically, I installed longjohn and was then able to get the full async stack trace to figure out the underlying cause (rabbit.js in my case).

Categories : Node Js

Node.js cluster master process reboot after got kill & pgrep?
I suggest you not to monitor the master process in your cluster, because you won't be able to handle SIGKILL. You can use upstart, monit or forever for this. Regarding to your second question, you can set the process title: if (cluster.isMaster) process.title = 'master'; ... } See docs for more info.

Categories : Javascript

wso2 data services server - cluster node and domain
Your assumption is correct. The WSO2 Elastic Load Balancer wiki has a lot more information on clustering. See here for more information.

Categories : Misc

ECMAScript 6 modules in Node.JS
You can use Continuum, which is an ES6 virtual machine written in (current) JavaScript.

Categories : Javascript

Sandboxing Node.js modules - can it be done?
Okay, so I thought about it some more today, and I think I have a basic strategy: var require = function(module) { throw "Uh-oh, untrusted code tried to load module '" + module + "'"; } var module = null; // use similar strategy for anything else susceptible var loadUntrusted = function() { eval(code); } Essentially, we just use variables in a local scope to hide the Node API from eval'ed code, and run the code. Another point of vulnerability would be objects from the Node API that are passed into untrusted code. If e.g. a buffer was passed to an untrusted object/function, that object/function could work its way up the prototype chain, and replace a key buffer function with its own malicious version. That would make all buffers used for e.g. File IO, or piping system command

Categories : Javascript

Mock node.js modules
I've used mockery with great success, although it can get really tedious depending on what you want to mock. However, your setup seems kinda wacky. If you want to unit test layer 1, you should only need to mock layer 2, and there shouldn't be any (direct) connection between layer 1 and layer 3.

Categories : Javascript

How do I use customized node modules?
Well you can host them @ the npm repository itself. In fact, anyone can and the best thing about it, is that you can just download your own package through npm install <your package name>. To get started, you need to do this like so: Go to your package directory, I see you've already made a package.json file there, so thats good, but its a part of the process. Since you've already made it, I won't elaborate Run this command npm init Fill out the data it wants. One interesting part is the point of entry parameter, make sure you think about it before you put something down. After that, make sure that you have a npm username, if not run npm adduser and go through the process Then you simply npm publish You should not be able to search for your module using the module you named it.

Categories : Javascript

d3js cluster layout dendogram node links to custom depths
You're using the .date attribute to determine the y position of the nodes, so you need to do the same to determine the start/end points of the links. You can modify the projection of the diagonal to also use .date (if it's present) to determine the y position: var diagonal = d3.svg.diagonal() .projection(function(d) { return [d.x, d.date ? +d.date : d.y]; });

Categories : D3 Js

Parallel Flow in Node Modules
async is the de facto module for these kind of tasks, go with it. No, it is not mandatory for those function to be asynchronous. I belive all your questions could be answered with this: http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/, where the following is the important point: Node.js keeps a single thread for your code however, everything runs in parallel except your code.

Categories : Node Js

In node, why do some modules export functions?
One consideration may be the Module module's caching feature. From the docs; my emphasis: Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file. Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles. If you want to have a module execute code multiple times, then export a function, and call that function.

Categories : Node Js

Node objects/modules information
GLOBAL is the keyword for global variables. So in your case you can do something like GLOBAL.rooms = rooms; After this is done, in any other parts of the app, you can access the rooms like this var rooms = GLOBAL.rooms;

Categories : Node Js

Install node modules error
Umm, it's npm install not node install. node is used to execute a JavaScript file. npm is used to run tasks related to package/module management.

Categories : Javascript

Importing node-modules with TypeScript
TypeScript needs to know that http is present. Follows these two steps Download the node.d.ts file from here : https://github.com/borisyankov/DefinitelyTyped/tree/master/node At the top of your file add: /// <reference path="node.d.ts" /> PS: See a sample test file : https://github.com/borisyankov/DefinitelyTyped/blob/master/node/node-tests.ts

Categories : Node Js

How to handle modules in an asynchronous app in node.js
var declarations are not scoped to loops, and should be made at the top of file/function var syncer = require('dropsite_server/dbox_sync') , client , websites_to_sync = [] //what is this used for? for (var i = 0; i < users.length; ++i) { if (users[i].access_token !== undefined) { client = dboxApp.client(users[i].access_token) syncer.doSync(client, users[i].website, users[i].access_token.uid); } } The reason the last item gets processed twice is that the doSync function sets module level variables that get overwritten each time you call it. The way to fix this is to pass the variables to the function instead exports.doSync = function (client, website_domain, uid) { sync_data_path = sync_data_path + "_" + uid remote_sync_dirs = website_domain async.series([

Categories : Javascript

Are Couchbase multi-node cluster a single point of failure when using PHP Client Library?
You can pass list of known endpoints of the cluster to constructor as array $hosts = array('example.com', 'example.org:8091'); $cb = new Couchbase($hosts, $user, $passwd, $bucket);

Categories : PHP

Hadoop & Hbase Installation and configuration Issues in a single node cluster on window
Create a directory, say /home/hadoop/workspace/temp_dir and add the property hadoop.tmp.dir with this directory as its value into your core-site.xml file. Then change the permissions of /home/hadoop/workspace/data_dir and /home/hadoop/workspace/temp_dir to 755 and restart Hadoop.

Categories : Hadoop

Webstorm don't recognize node.js third party modules
It seems that the '..params' module is exporting a constructor function that constructs an object which has kioskParams as an attribute. And the constructor itself doesn't have an attribute called kioskParams. It can be easier understood if you write it like this: var SyParams = require('../params'); // The module exports a constructor ... var syParams = new SyParams(); // You construct the actual object syParams.kioskParams; //Then you access its members

Categories : Node Js

Creating Callbacks for required modules in node.js
Yes, you can have a callback from your module. It's as simple as function funcWithCallback(args, callback){ //do stuff callback(); } While I don't know what you are trying to accomplish, the while loop looks suspicious. You probably should invest in the async package from npm. async on github EDIT: I felt the need to clarify something. While the above function does in fact intend the callback is used instead of the return value, it's not exactly async. The true async approach is to do something like this: function funcWithCallback(args, callback){ process.nextTick(function() { //do stuff callback(); }); } This allows the called function to exit and defers the execution of the logic of that function until the next tick.

Categories : Javascript

Is there a good way to globally require node modules?
Yes, globals are bad and the programming community at large has embraced avoidance of global variables as a sound practice. If you do this, everyone who ever looks at your code is going to do a massive facepalm immediately. But yes, you can create globals just by omitting the var keyword as your example indicates. Don't Repeat Yourself is about code, not about dependency declaration.

Categories : Node Js

Is it possible to replace or alias core modules in Node.js?
Not really. require is a core var which is local to each module, so you can't stub it, because Node will give the untouched require var to the loaded module. You could run those things using the vm module. However, you would have to write too much code to do a "simple workaround" (give all needed variables to the request module, stub the needed ones to work properly, etc, etc...).

Categories : Node Js



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.