2

I am very new to node js. I am trying to create a simple coap client and server using node js. I could create a server on which a text file resides. I want to access that from the client.

var coap = require('coap');
var str = "";
var req = coap.request("coap://localhost:5683");
req.on("response", function(chunk){
    str +=chunk;    
    console.log(str);   
    chunk.pipe(process.stdout);
});

This code is giving me the output as [object Object]. How do I get the string form of this.

2 Answers 2

5

You can use JSON.stringify and JSON.parse to do that. Here the documentation.

Sign up to request clarification or add additional context in comments.

6 Comments

Using JSON.stringify is also giving me the same output. :(
Wait. Is that chunk a node buffer? My fault, I didn't get it before. Is it?
yes it is. Its a chunk of data from the file on the server.
What does the file contain? If you want to parse an object read from that file, you have to write something there that makes sense...
Its is simple text file
|
0

for general purpose : JSON.stringify(obj);

if object includes functions : require("util").inspect(obj);

read it here : https://nodejs.org/en/knowledge/getting-started/how-to-use-util-inspect/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.