how to correctly reference a js file in a ejs file in Angular

北城以北 提交于 2019-12-12 01:28:46

问题


This is my project structure:

public -js --angular.min.js --angularApp.js templates -views --index.ejs

This is my index.ejs file:

<html>
    <head>
        <title>My Angular app!</title>
        <script type='text/javascript' src="../../public/js/angular.min.js"></script>

    </head>

    <body ng-app="flapperNews" ng-controller="MainCtrl">
        <div>
            {{test}}
        </div>
        <script type='text/javascript' src="../../public/js/angularApp.js"></script>
    </body>

</html>

When I run this I get a 404 not found error on the js files.

I tried adding this to middleware.js in the routes folder:

app.use("/public/js/angularApp.js", express.static(__dirname + '/public/js/angularApp.js'));

But this didnt work, I also tried using this path:

/public/js/

But also didn't work.

How can I correctly reference a Javascript file in a ejs file?


回答1:


The public folder is you root directory from the vistor perspective. So in your case it should be

<script type='text/javascript' src="/js/angular.min.js">


来源:https://stackoverflow.com/questions/33152484/how-to-correctly-reference-a-js-file-in-a-ejs-file-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!