简体   繁体   English

Heroku没有加载js脚本404

[英]Heroku not loading js script 404

I am trying to build my first web app using MEAN on Heroku. 我正在尝试使用Heroku上的MEAN构建我的第一个Web应用程序。 I followed their guide to getting a sample app running. 我按照他们的指南来运行示例应用程序。 Then I downloaded the sample app code and altered it to load the login page. 然后我下载了示例应用程序代码并对其进行了更改以加载登录页面。 Unfortunately, I can't get the my app.js file to load. 不幸的是,我无法加载我的app.js文件。 This is the angular script. 这是角度脚本。 In the main directory I have index.js that is running express. 在主目录中,我有运行express的index.js。 Anyways, I am able to get the .ejs .css and img files to load but this script wont. 无论如何,我能够加载.ejs .css和img文件,但这个脚本不会。 I am stuck. 我被卡住了。 I need to be able to get past this to tinker enough to start learning the stack. 我需要能够通过这个来修补足以开始学习堆栈。

Script is in the public directory with the other files that get loaded. 脚本位于公共目录中,其他文件已加载。 Code looks okay? 代码看起来还好吗? Don't know why I get 404 on the script. 不知道为什么我在脚本上得到404。

Any help is much appreciated! 任何帮助深表感谢!

var express = require('express');
var app = express();

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/', function(request, response) {
  response.render('pages/index');
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});



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

Turns out changes weren't being pushed to the server. 原来没有更改被推送到服务器。 That's all folks! 这就是所有人!

You just need to move app.js to the public folder. 您只需将app.js移动到公用文件夹即可。 This line app.use(express.static(__dirname + '/public')); 这行app.use(express.static(__dirname + '/public')); in your index.js tells express to serve static assets out of the /public folder. 在你的index.js中告诉express要从/public文件夹中提供静态资源。 Everything else in your project will be "hidden" on the server unless you expose it. 除非您公开它,否则项目中的其他所有内容都将“隐藏”在服务器上。

You could create a js folder in public and move app.js there. 您可以在公共场所创建一个js文件夹,然后在那里移动app.js. Then change the reference in index.ejs from src="/app.js" to src="/js/app.js". 然后将index.ejs中的引用从src="/app.js"更改为src =“/ js / app.js”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM