tencent cloud

All product documents
TencentDB for Redis®
Node.JS Connection Sample
Last updated: 2024-11-05 10:10:15
Node.JS Connection Sample
Last updated: 2024-11-05 10:10:15
Preparations before running: Run the following command to install node-redis: npm install hiredis redis
Sample code:
var redis = require("redis");

/**For the following parameters, enter your Redis instance's private IP, port number, instance ID and password*/
var host = "192.xx.xx.2",
port = "6379",
instanceid = "c53xx52f-55dc-4c22-a941-630xxx88",
pwd = "12as6zb";
// Connect to Redis
var client = redis.createClient(port, host, {detect_buffers: true});
// Redis connection error
client.on("error", function(error) {
console.log(error);
});
// Authenticate
client.auth(instanceid + ":" + pwd);

/**You can start manipulating the Redis instance */
// Set the Key
client.set("redis", "tencent", function(err, reply){
if (err) {
console.log(err);
return;
}
console.log("set key redis " + reply.toString() + ", value is tencent");
});

// Get the Key
client.get("redis", function (err, reply) {
if (err) {
console.log(err);
return;
}
console.log("get key redis is:" + reply.toString());
// End the program and close the client
client.end();
});
Execution result:
NodeJS-1.jpg


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon