v8rs
v8rs

Reputation: 307

Return a string in a HTTP response as JSON using express JS

I want to return a string in a HTTP response as JSON using express JS. I am trying this but I am getting error. What am I doing wrong?

msg = '{"foo": "bar"}';
msgJSON = JSON.parse(msg);
res.status(200).send(msgJSON);

I need to be a string because it is generated concatenating key-value pairs

Upvotes: 0

Views: 1023

Answers (1)

Dan Crews
Dan Crews

Reputation: 3597

Based off your edit, you don't need to parse the object. You can just send the data:

res.set('content-type', 'application/json');
res.status(200).send(msg);

Upvotes: 1

Related Questions