0

i'm a beginner in Node.js and i dont understand what is this **/mockServiceWorker.js **file? The problem is that my req.url is changing to /mockServiceWorker.js after some time, even though the first couple of seconds everything is working fine and the req.url is correct(file2.html).

My code

import fs from 'fs';
import http from 'http';

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

    let path = req.url;
    if (req.url == "/") path = "file2.html";
    if (req.url = "/mockServiceWorker.js") req.url = path;
    console.log(path)
    let text = await fs.promises.readFile(path, "utf8");
    res.writeHead(200, { "Content-Type": "text/html" });
    res.write(text)
    res.end();
}).listen(3000)

**The error: **

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

**[Error: ENOENT: no such file or directory, open '/mockServiceWorker.js'] {
**  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/mockServiceWorker.js'
}

I've tried to use conditional statement, but it didnt help

if (req.url = "/mockServiceWorker.js") req.url = path;

I tried to unistall it via npm, nothing helped

0 Answers0