html不能调用file.js
我创build了我的第一个server.js,index.html和test.js. 当我运行节点时,索引页面不能调用test.js
var http = require('http'); var fs = require('fs'); var server = http.createServer(function(req, res) { fs.readFile('./index.html', 'utf-8', function(error, content) { res.writeHead(200, {"Content-Type": "text/html"}); res.end(content); }); }); server.listen(8080);
的index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>PLAN</title> <script type="text/javascript" scr="test.js"></script> </head> <body> hello </body> </html>
test.js
"use strict"; alert("aa1");
有你的两个问题。
-
不正确的
<script>
标记属性,在index.html
文件中从scr
更改为src
-
您的服务器需要提供JavaScript文件,即
fs.readFile('./test.js')
,所以请将您的server.js
更改为以下内容。
var http = require('http'); var fs = require('fs'); var path = require('path'); http.createServer(function (request, response) { var filePath = '.' + request.url; if (filePath == './') filePath = './index.html'; var extname = path.extname(filePath); var contentType = 'text/html' if (extname == '.js') contentType = 'text/javascript'; fs.readFile(filePath, function(error, content) { response.writeHead(200, { 'Content-Type': contentType }); response.end(content, 'utf-8'); }); }).listen(8080); console.log('Server running at http://127.0.0.1:8080/');
我认为这应该工作。
- 有没有一种方法来定义一个没有.save()方法的mongoose模型?
- Aurelia应用程序“Hello World”根本不工作
- CORS问题:请求的资源上没有“Access-Control-Allow-Origin”标题
- 如何使用JWT使用NodeJS客户端库访问Google Directory(Admin SDK)?
- npm安装不会创builddist文件夹
- CORS用node.js和socket.io阻塞
- Node.js:如何使用MS Bot框架在Slack中发送直接消息?
- 如何调整图像大小并使用nodejs中的multer上传到s3并使用easy-image npm模块?
- xmpp s2s如何工作?