john doe
john doe

Reputation: 9660

Setting the Response Header Using Express Node

I am using the following code to set the response header:

var express = require("express");
var app = express();

app.get("/",function(req,res){

  res.header('Content-Type','application/json');

  var task = { title :"Do the grocery" }
  res.send(JSON.stringify(task));
});

When I see in my response in Google Chrome I don't see the response header "Content-Type" being set. Am I doing something wrong?

Upvotes: 1

Views: 4219

Answers (1)

Paul
Paul

Reputation: 36319

That's not a method afaik, should be:

res.set('Content-Type', 'application/json');

As a sidenote, if you call res.json() you can just pass it an object and it'll json stringify it for you.

Upvotes: 2

Related Questions