Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTTP/HTTPS节点用HTML提供Javascript_Javascript_Html_Node.js - Fatal编程技术网

HTTP/HTTPS节点用HTML提供Javascript

HTTP/HTTPS节点用HTML提供Javascript,javascript,html,node.js,Javascript,Html,Node.js,我有一个非常简单的https Nodejs服务器,它提供index.html,其中包含对Javascript文件的请求。我似乎无法让浏览器识别Javascript文件 <html> <head> <script src="deviceMotion.js"></script> </head> <body> </body> </html> 我的文件的结构使app.js与名为“sr

我有一个非常简单的https Nodejs服务器,它提供index.html,其中包含对Javascript文件的请求。我似乎无法让浏览器识别Javascript文件

<html>
  <head>
    <script src="deviceMotion.js"></script>
  </head>
  <body>
  </body>
</html>
我的文件的结构使app.js与名为“src”的文件夹位于同一目录中,并且在src下有index.html和deviceMotion.js。
如何根据传入的请求控制我提供的文件和时间?我如何区分为提供正确的文件而提出的请求?我尝试过解析req.baseUrl和req.path,但两者都未定义。

您的Node.js服务器总是返回一个HTML文件,并将内容类型设置为HTML,因此当您的网站请求JavaScript文件时,它会返回一个HTML文件,导致
Uncaught SyntaxError:Unexpected token“Node.js服务器中没有任何代码来处理传入的文件请求。它只返回HTML文件,因此当浏览器请求javascript文件时,web服务器将返回HTML文件。因此,它为您提供了
uncaughtsyntaxerror:Unexpected标记'
const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    console.log("request received");
    console.log(req.headers.referer);
    fs.readFile('./src/index.html', function (error, data) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(data);
    });
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
const server = http.createServer((req, res) => {
    console.log("request received");
    console.log(req.headers.referer);
    fs.readFile('./' + req.url, function (error, data) {
        res.end(data);
    });
});