상세 컨텐츠

본문 제목

How to send formatted json in expressjs?

Code Snippets/NodeJS

by w3labkr 2021. 12. 8. 22:20

본문

app.js

app.set('json spaces', 4);

 

You're going to have to set the Content-Type to application/json like this

app.get('/', function (req, res) {
    users.find({}).toArray(function(err, results){
        res.header("Content-Type",'application/json');
        res.send(JSON.stringify(results, null, 4));
  });
});

node and express send json formatted

 

node and express send json formatted

I'm trying to send formatted json with express. Here is my code: var app = express(); app.get('/', function (req, res) { users.find({}).toArray(function(err, results){ // I have try both ...

stackoverflow.com