简体   繁体   English

脚本src通过nodejs + express成为玉器中的url

[英]script src becomes a url in jade with nodejs + express

I am working on a web application with nodejs + jade. 我正在使用nodejs + jade开发Web应用程序。

I want to include a js file into my jade page. 我想在我的翡翠页面中包含一个js文件。 But it doesn't work as I thought it should do. 但这不起作用,我认为应该做。

I include three js files: 我包括三个js文件:

script(src='//cdn.bootcss.com/jquery/1.11.3/jquery.min.js')
script(src='//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js')
script(src="async.js")

The first two work well (bootstrap files). 前两个工作良好(引导文件)。 But the browser will claim at the third one "GET http://127.0.0.1:3000/sample/auth/async.js 404 (Not Found)". 但是浏览器将在第三处声明“ GET http://127.0.0.1:3000/sample/auth/async.js 404(未找到)”。 It seems that some thing changes the src to a url with the base url "http://127.0.0.1:3000/sample/auth/" 似乎有些事情将src更改为带有基本URL“ http://127.0.0.1:3000/sample/auth/”的URL

"127.0.0.1" is localhost and port "3000" is the port the server is listening to, and " http://127.0.0.1:3000/sample " is the entrance of my web application. “ 127.0.0.1”是本地主机,端口“ 3000”是服务器正在侦听的端口,“ http://127.0.0.1:3000/sample ”是我的Web应用程序的入口。

So I guess there is a configuration in the project to force the src be a url like that. 因此,我猜项目中有一个配置可以强制src是这样的url。 And I don't know how to include local js files. 而且我不知道如何包括本地js文件。 Can anyone help? 有人可以帮忙吗? Thanks! 谢谢!

You should put async.js in /public/scripts in your Express app. 您应该在Express应用程序的/public/scripts中放入async.js You'll probably need to create this directory, but you'll see that your app's local CSS files are located there in /public/stylesheets . 您可能需要创建此目录,但是您会看到应用程序的本地CSS文件位于/public/stylesheets

Once you do this, reference your async.js file like this script(src='/scripts/async.js') . 完成此操作后,请像此script(src='/scripts/async.js')一样引用您的async.js文件。

This is the preferred method for serving local CSS and JavaScript. 这是服务于本地CSS和JavaScript的首选方法。 You can see in app.js see that Express looks there for any static resources. 您可以在app.js看到Express在那里查找任何静态资源。

app.use(express.static(path.join(__dirname, 'public')));

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

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