JavaScript不加载在快递/节点应用程序

我正在做一个小MEAN的应用程序

我的JavaScript内的HTML不加载

下面是我的快车路线

app.get("/getuser/:id",userRoute.getUser); 

目录结构 nodejs / view

****服务器文件****

我的路线function

 var getUser=function(req,res) { console.log("hieieieiei"); res.sendFile(path.join(__dirname + '/view/index.html')); }; 

我的html文件名是index.html ,我的js文件名是test.js。 两者都在文件夹命名的视图

我有这在我的HTML提到。 有人可以帮忙吗?

的index.html

  <html> <head lang="en"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> <script type="text/javascript" src="test.js"></script> <meta charset="UTF-8"> <title> dfdfdfddddddddddddddddddd</title> </head> <body> <button onclick="clicker()"></button> </body> </html> 

test.js

 function clicker() { alert("hi "); } 

我的html文件名是index.html,我的js文件名是test.js。 两者都在文件夹命名的视图。

根据http://expressjs.com/en/starter/static-files.html,您需要通过使&#x7528;express.static来明确表示使用哪个目录或目录来提供静态文件。

所以,你可以尝试:

  1. 在您的项目中创build一个公用文件夹
  2. 将test.js从您的视图文件夹移到步骤1中创build的公用文件夹

然后更新您的configuration代码,使其包含以下内容:

 app.use(express.static('public')); 

希望这会做的伎俩。