如何使用node.js在html中执行外部javascript文件

时间:2022-08-28 18:59:08

I am a complete node.js newbie and struggling with the basics.I have a html file and I would like to call external javascript file in the html page using node.js in local host environment.

我是一个完整的node.js新手和苦苦挣扎的基础知识。我有一个html文件,我想在本地主机环境中使用node.js在html页面中调用外部javascript文件。

3 个解决方案

#1


1  

Your main file should check whether the requested url is asking for html or js or css file.

您的主文件应检查请求的URL是否要求html或js或css文件。

var server = http.createServer(function(request, response) {
console.log("Received Request: " + request.url);
    if(request.url.indexOf('.html') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Html Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/html'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else if(request.url.indexOf('.js') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Javascript Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/javascript'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else if(request.url.indexOf('.css') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Css Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/css'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else {
        console.log("Inside the inside else statement");
        response.writeHead(404, {"COntent-type":"text/plain"});
        response.end("No Page Found");
    }
}); 

After That you can include external javascript and css files in html files.

之后,您可以在html文件中包含外部javascript和css文件。

#2


0  

You just add the script tag into the html:

您只需将脚本标记添加到html中:

<script type="text/javascript" href="/path/to/src.js"></script>

#3


0  

I think I have understood your real interest.

我想我已经理解了你真正的兴趣。

<script src="script.js"></script>

If you are trying to get a script tag (like that one above) working on your html, you should write a "router" inside your Node.js http.createServer callback. When the browser find the script tag, it will send a specific request to the server and then you can send the javascript code back on the response.

如果你想在你的html上获得一个脚本标签(就像上面那个),你应该在你的Node.js http.createServer回调中写一个“路由器”。当浏览器找到脚本标记时,它会向服务器发送特定请求,然后您可以在响应中发回javascript代码。

I have written a way to do it on a similar question here: How to include javascript on client side of node.js?.

我在这里写了一个类似问题的方法:如何在node.js的客户端包含javascript?

#1


1  

Your main file should check whether the requested url is asking for html or js or css file.

您的主文件应检查请求的URL是否要求html或js或css文件。

var server = http.createServer(function(request, response) {
console.log("Received Request: " + request.url);
    if(request.url.indexOf('.html') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Html Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/html'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else if(request.url.indexOf('.js') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Javascript Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/javascript'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else if(request.url.indexOf('.css') != -1) {
        fs.readFile("game" + request.url, function (error, data) {
            if (error) {
                response.writeHead(404, {"COntent-type":"text/plain"});
                response.end("No Css Page Found.");
            } else{
                response.writeHead(200, {'Content-Type': 'text/css'});
                response.write(data);
                response.end(); 
            }

        });
    }
    else {
        console.log("Inside the inside else statement");
        response.writeHead(404, {"COntent-type":"text/plain"});
        response.end("No Page Found");
    }
}); 

After That you can include external javascript and css files in html files.

之后,您可以在html文件中包含外部javascript和css文件。

#2


0  

You just add the script tag into the html:

您只需将脚本标记添加到html中:

<script type="text/javascript" href="/path/to/src.js"></script>

#3


0  

I think I have understood your real interest.

我想我已经理解了你真正的兴趣。

<script src="script.js"></script>

If you are trying to get a script tag (like that one above) working on your html, you should write a "router" inside your Node.js http.createServer callback. When the browser find the script tag, it will send a specific request to the server and then you can send the javascript code back on the response.

如果你想在你的html上获得一个脚本标签(就像上面那个),你应该在你的Node.js http.createServer回调中写一个“路由器”。当浏览器找到脚本标记时,它会向服务器发送特定请求,然后您可以在响应中发回javascript代码。

I have written a way to do it on a similar question here: How to include javascript on client side of node.js?.

我在这里写了一个类似问题的方法:如何在node.js的客户端包含javascript?