简体   繁体   中英

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, '127.0.0.1:9042' or '127.0.0.1'

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

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 username = req.body.username;
  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 {
      console.log("success");
    }
  });
});

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

cd apache-cassandra-3.0.6 -> cd bin -> ./cassandra

What am I missing here?

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 PlainTextAuthProvider = cassandra.auth.PlainTextAuthProvider;
var client = new cassandra.Client({ contactPoints:['127.0.0.1:9042'], 
                                    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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM