0 votes
1 view
in Web Technology by (14.5k points)

I am trying to use a manifest.json file, which refuses to work if the type isn't set to application/json.

I have a nginx server running node/express.

I put the manifest.json in my public folder and the server reads the type as text/plain, and throws an error.

Under /etc/nginx/config/ there is already a line that reads:

application/json                      json;

I even tried serving the file with express and specifying the header:

var manifest = fs.readFileSync('routes/manifest.json', 'utf8');

router.get('/manifest.json', function(req, res) {

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



And that still reads it as text/plain.

I'm guessing it's something I need to do with nginx, but all the suggestions I've found talk about files I don't have.

1 Answer

0 votes
by (25.1k points)

When the fs module reads your existing manifest.json file, it is converting it into one large utf-8 string, instead of actual JSON.

To the human eye, it looks like regular JSON, but it’s actually just a bunch of JSON inside a string.

To the server, it just sees the string and sets the mime type as it should, as text/plain, since it technically is.

You could try replacing res.json(manifest); with res.send(JSON.stringify(manifest));, which should take the value that fs got from manifest.json, and attempt to convert it to properly formatted JSON with the proper mime type.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !