【问题标题】:Express js produces 404 for .js file that clearly existsExpress js 为明显存在的 .js 文件生成 404
【发布时间】:2015-02-18 08:26:44
【问题描述】:

我正在使用 express-generator 自动生成的布局制作一个简单的 node/express 应用程序:

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

在“/public/javascripts”目录中有一个“gauge-main.js”文件。

此文件在我的“index.jade”文件中引用,位于“/views”目录中:

extends layout

block head
        head
                meta(charset='utf-8')
                title Index
                link(rel='stylesheet', href='/stylesheets/gauge-index.css', type='text/css', media='screen', charset='utf-8')
block body
        body
                script(src='/public/javascripts/gauge-main.js', type='text/javascript', charset='utf-8')

当应用程序运行时,一些 '*.css' 文件被正确链接,但 'gauge-main.js' 产生 404 错误:

> ase@0.0.0 start /srv/node/ase
> node ./bin/www

GET / 200 231.694 ms - 424
GET /stylesheets/style.css 200 4.683 ms - 110
Warning: Unexpected block "content"  on line 3 of /srv/node/ase/views/error.jade. This block is never used. This warning will be an error in v2.0.0
GET /public/javascripts/gauge-main.js 404 12.587 ms - 154
GET /stylesheets/gauge-index.css 200 13.849 ms - 155
GET /stylesheets/gauge-simple.css 200 2.074 ms - 1025
GET /stylesheets/gauge-small.css 200 1.919 ms - 1014
GET /stylesheets/gauge-grayscale.css 200 1.857 ms - 1039
Warning: Unexpected block "content"  on line 3 of /srv/node/ase/views/error.jade. This block is never used. This warning will be an error in v2.0.0
GET /favicon.ico 404 10.821 ms - 154
Warning: Unexpected block "content"  on line 3 of /srv/node/ase/views/error.jade. This block is never used. This warning will be an error in v2.0.0
GET /favicon.ico 404 6.519 ms - 154

顺便说一句,请忽略图标的其他 404。

我认为由于使用相对路径('/stylesheets/...')正确链接了“*.css”文件,因此链接的“gauge-main.js”应该也是如此同样的方式('/javascripts/gauge-main.js')。

有什么想法吗?

【问题讨论】:

    标签: node.js express http-status-code-404 pug


    【解决方案1】:

    我猜你在做app.use(express.static(__dirname + '/public')); 因为你的css 被正确地提供了。因此,您需要做的是将脚本的 src 值更改为以 /javascripts/ 而不是 /public/javascripts/ 开头。

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 2011-09-11
      • 2012-05-26
      • 2016-01-24
      • 2017-01-19
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多