简体   繁体   English

节点静态js文件未刷新

[英]Node static js file isn't refreshing

I'm trying to build my first node app and am having problems with the javascript file, but not my css so I'm very confused. 我正在尝试构建我的第一个节点应用程序,但是javascript文件有问题,但css却没有,所以我很困惑。 Here is my app.js: 这是我的app.js:

var express = require("express");
var app = express();
app.set("view engine", "ejs");
app.use(express.static(__dirname + '/public'));

I couldn't load my js file until I added line 4, where I saw in the chrome console "connected", which was from my js file. 在添加第4行之前,我无法加载我的js文件,在chrome控制台中看到的“已连接”是我的js文件。 The problem that I am having is that since I added line 4, express.static my js file hasn't changed and everything that I have added in the file is not showing in the chrome resources tab. 我遇到的问题是,自从添加第4行以来,express.static我的js文件没有更改,并且我在文件中添加的所有内容均未显示在chrome资源标签中。 Strangely though, my css file is working correctly, so I'm a little bit stumped. 但是奇怪的是,我的css文件可以正常工作,所以我有些困惑。

I am trying linking to the js file like: 我正在尝试链接到js文件,例如:

<script src="javascripts/main.js"></script>

I'm not really sure why its not working properly, I tried restarting the server and have since installed nodemon which hasn't worked either. 我不太确定为什么它不能正常工作,我尝试重新启动服务器,然后安装了nodemon,但该服务器也无法正常工作。 my folder structure is 我的文件夹结构是

app.js
public
    stylesheets
        main.css
    javascripts
        main.js

I have looked up the problem and have only found people not connecting to static files, not once connected they aren't showing any saved changes 我查了一下这个问题,只发现人们没有连接到静态文件,一旦连接就没有显示任何保存的更改

I think you must disable cache in your browser. 我认为您必须禁用浏览器中的缓存。 Because browser think that file is not to old to be refresh from server and use cached file. 因为浏览器认为该文件不老才能从服务器刷新并使用缓存的文件。
For Chrome - open console by F12 (also in other browser works) and check it. 对于Chrome浏览器-按F12打开控制台(在其他浏览器中也可以使用)并进行检查。

在此处输入图片说明

Try using an absolute path to ensure static assets get loaded properly. 尝试使用绝对路径以确保正确加载静态资产。 Using relative paths (eg javascripts/main.js ) can be problematic because of the way the browser converts them to absolute paths (it takes into account the full url, including any nested routes). 使用相对路径(例如javascripts/main.js )可能会出现问题,因为浏览器将它们转换为绝对路径的方式(考虑了完整的url,包括任何嵌套的路径)。

So for example if you have a route /foo/bar set up and you are using relative paths, when you visit /foo/bar in your browser, the browser will try to access /foo/bar/javascripts/main.js . 因此,例如,如果您设置了一条路由/foo/bar ,并且正在使用相对路径,那么当您在浏览器中访问/foo/bar ,浏览器将尝试访问/foo/bar/javascripts/main.js

The problem you are facing is already answered by the library like webpack. 像webpack这样的库已经解决了您面临的问题。 It does hot reloading. 它会热装。 Whatever the changes you make to your web app, will be loaded into client and the files will be patched to update the changes without having to restart the server. 无论您对Web应用程序所做的更改是什么,都将被加载到客户端,并且将对文件进行修补以更新更改,而无需重新启动服务器。 You will have updated scripts running in the browser. 您将在浏览器中运行更新的脚本。

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

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