T.O T.O - 2 years ago 223
Node.js Question

How to connect Node.js with Cassandra?

I've been trying to connect Node.js with Cassandra on localhost for 50 years (feel like it), but haven't figured out how they work together. I would appreciate any suggestion that could lead to solution.

Project Directory:

project - app - some files
\- build - index.js, index.html, etc.(I start the server by "node index.js")
\- node_modules - some modules
\- signup - some files to be minified
\- signin - some files to be minified
\- apache-cassandra-3.0.6 - bin, conf, etc.(downloaded from tarball)
\- package.json
\- webpack.config.js

Webpack is working without any problem, so the problem doesn't exist in webpack configuration.

I can insert data using cqlsh, so the problem isn't the model of data structure.

I believe the problem is my lack of knowledge about how to use Node.js and Cassandra together.

My process to connect Node.js with Cassandra:

  1. go to build directory

  2. node index.js

  3. open localhost:3000 and find the home page, routed by express.js, displayed with no problem.

  4. go to sign-up page and submit a form

  5. error (no data inserted)

I'm not sure which contact point is correct, '' or ''

var client = new cassandra.Client({contactPoints:['']});
var client = new cassandra.Client({contactPoints:['']});

I set my router like this.

var router = express.Router();
var insertUser = 'INSERT INTO keyspace.users (username, create_time, email, password) VALUES (?, ?, ?, ?);';

router.get"/", function(req, res) {
res.sendFile(__dirname + '/index.html'); // This is working.

router.post("/signup", function(req, res) {
var email = req.body.email;
var password = req.body.password;
client.execute(insertUser, [username, now(), email, password],
{ prepare: true }, function(err) {
if (err) {
console.log("error"); // I receive error.
} else {

Should I keep the cassandra running in the background like this?

cd apache-cassandra-3.0.6
cd bin

What am I missing here?

Answer Source

Based the error message you provided, it looks like you have PasswordAuthenticator authentication or some other authenticator set up on your Cassandra instance. Check your 'authenticator' property in your cassandra.yaml file, is it set to PasswordAuthenticator, AllowAllAuthenticator, or something else?

If using PasswordAuthenticator, you can specify credentials by passing a PlainTextAuthenticator instance to your client options as authProvider:

var PlainTextAuthenticator = cassandra.auth.PlainTextAuthProvider;
var client = new cassandra.Client({ contactPoints:[''], 
                                    authProvider: new PlainTextAuthProvider('cassandra', 'cassandra')};

The default user made available when using authentication is 'cassandra' with password 'cassandra', but you can change this by following Configuring Authentication.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download