我已阅读此链接 Node.js - external JS and CSS files (just using node.js not express)对我有所了解,但仍然不知道将它们插入何处。考虑下面的“Hello World”node.js 应用程序,头部部分的 2 行链接和脚本将不起作用。我猜是因为它们还不是网络引用。那么,我如何将它们包括在内?如果我确实喜欢链接建议,它们会在头部部分之外吗?
var http = require('http');
var html =
'<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'+
'<link rel="stylesheet" type="text/css" href="./mystyle.css">'+
'<script src="./myscript.js"></script>'+
'</head>'+
'<body>'+
'<p>Hello World!</p>'+
'</body>'+
'</html>';
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(html);
response.end();
}).listen(80);
最佳答案
您应该使用 response.writeHead,而不是 request.writeHead。
此外,无论js或css请求如何,当前代码将返回相同的文件。
关于javascript - 如何将 css 和脚本文件引用到 node.js 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22874913/