javascript - web worker throwing 404 when executing - Stack Overflow

My project structure looks like jsclient.jsscript1.jswebWoker.jsnode_modules.gitignoreindex.htmlT

My project structure looks like

js
  /client.js
  /script1.js
  /webWoker.js
node_modules
.gitignore
index.html

The main.html has it included as well in

<script type="text/javascript" src="js/script1.js"></script>
<script type="text/javascript" src="js/client.js"></script>
<script type="text/javascript" src="js/webWoker.js"></script>

My script1.js looks like

if (window.Worker) {
    console.log("uri: " + document.documentURI);
    var myWorker = new Worker("myworker.js");
    myWorker.postMessage("hello");
    console.log(myWorker);

    myWorker.onmessage = function (e) {
        result.textContent = e.data;
        console.log('Message received from worker: ' + result.textContent);
    };
}

and my webWorker.js looks like

onmessage = function (e) {
    console.log('Message received from main script');
    var result = "#27ae60";
    console.log('Posting message back to main script');
    postMessage(result);
};

I use node.js for this project and run it via npm start, and when I run this in browser I see

script1.js:81 GET http://localhost:8080/webWorker.js 404 (Not Found)
execute @ script1.js:81
img.onload @ script1.js:64

What is going wrong here?

My project structure looks like

js
  /client.js
  /script1.js
  /webWoker.js
node_modules
.gitignore
index.html

The main.html has it included as well in

<script type="text/javascript" src="js/script1.js"></script>
<script type="text/javascript" src="js/client.js"></script>
<script type="text/javascript" src="js/webWoker.js"></script>

My script1.js looks like

if (window.Worker) {
    console.log("uri: " + document.documentURI);
    var myWorker = new Worker("myworker.js");
    myWorker.postMessage("hello");
    console.log(myWorker);

    myWorker.onmessage = function (e) {
        result.textContent = e.data;
        console.log('Message received from worker: ' + result.textContent);
    };
}

and my webWorker.js looks like

onmessage = function (e) {
    console.log('Message received from main script');
    var result = "#27ae60";
    console.log('Posting message back to main script');
    postMessage(result);
};

I use node.js for this project and run it via npm start, and when I run this in browser I see

script1.js:81 GET http://localhost:8080/webWorker.js 404 (Not Found)
execute @ script1.js:81
img.onload @ script1.js:64

What is going wrong here?

Share Improve this question edited Apr 19, 2017 at 4:59 buræquete 14.7k4 gold badges53 silver badges95 bronze badges asked Apr 19, 2017 at 1:39 daydreamerdaydreamer 92.2k204 gold badges473 silver badges750 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

You don't have to include the worker script in your main page (it's even a bad idea), but the URI you pass to new Worker(URI) is relative to the current documentURI.

So in your case, it should be new Worker("/js/webWorker.js");.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744197377a4562720.html

相关推荐

  • javascript - web worker throwing 404 when executing - Stack Overflow

    My project structure looks like jsclient.jsscript1.jswebWoker.jsnode_modules.gitignoreindex.htmlT

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信