styler
styler

Reputation: 16481

How to return data response using express?

I am trying to run .get on a JSON file I've set up located at /scripts/src/data/*.json when I make the request I set the headers but I'm not sure how I actually return the resulting data or where I can view this request. Can someone offer any help?

JS

server.get('/scripts/src/data/*.json', function(req, res) {
    res.setHeader("Content-Type", "application/json");

    // return ??
});

Upvotes: 1

Views: 3698

Answers (2)

cshion
cshion

Reputation: 1213

You could use static middleware to serve your json files ,

server.use(express.static(__dirname + "/scripts/src/data"))
//other routes 

In client side , you just should request GET localhost:port/file.json

Upvotes: 2

earl3s
earl3s

Reputation: 2373

Try this:

server.get('/scripts/src/data/*.json', function(req, res) {
    res.setHeader("Content-Type", "application/json");
    res.status(200);
    res.json({
        hello: "world"
    });
    // return ??
});

Upvotes: 1

Related Questions