Simple http pdf file server CORS policy

Viewed 27

i am building simple app in vue that should diplay simple pdf file from local NAS. My idea was to install nodejs on NAS (synology) a run simple server like this:

var http = require('http');
var fs = require('fs');

console.log('Server will listen at :  127.0.0.1:3000 ');

http.createServer( (req, res)=> {

    const headers = {
        'Access-Control-Allow-Origin': '*', /* @dev First, read about security */
        'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
      };

    console.log("Port Number : 3000");
    // Change the MIME type to application/pdf
    res.writeHead(200, {"Content-Type": "application/pdf", headers});
     
    fs.readFile('index.pdf', (error,data) => {
        if(error){
            res.json({'status':'error',msg:err});
        }else{          
            res.write(data);
            res.end();       
        }
    });
}).listen(3000);

then i would simply make a call from my app (using pdf lib.) like this:

<template>
  <div class="hello">
    

<vue-pdf-embed :source="source1" />

  </div>
</template>



<script>

import VuePdfEmbed from 'vue-pdf-embed'

export default {
  name: 'HelloWorld',

  props: {
    msg: String
  },

  components: {
    VuePdfEmbed,
  },

    data() {
    return {
      source1: 'http://127.0.0.1:3000/',
      
    }
  }
}

</script>

The problem here is the CORS policy that gives me following error:

Access to fetch at 'http://127.0.0.1:3000/' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Can someone please give me any advise on how to solve this or if my approach even makes sense or if I should change it? I dont have much experience with CORS or networking (I am aware of security implications of CORS but for this application security is not really concern)

Thank you, J.

0 Answers
Related