无法在iisnode上提供静态.css文件

时间:2018-01-31 17:14:37

标签: css node.js express iis-7.5 iisnode

我无法弄清楚我做错了什么。

这是我的目录结构:

+projectName
  +node_modules
  +public
    +stylesheets
      -style.css
  +routes
    -index.js
  +views
    +partials
      -footer.hbs
      -header.hbs
    -index.hbs
    -results.hbs
  -.gitignore
  -app.js
  -package-lock.json
  -package.json
  -web.config

web.config文件如下:

<configuration>
<system.webServer>

    <!-- indicates that the app.js file is a node.js application
    to be handled by the iisnode module -->

    <handlers>
        <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
        <rules>

            <rule name="StaticContent">  
                <action type="Rewrite" url="public{{REQUEST_URI}}" logRewrittenUrl="true" />
            </rule>

            <rule name="DynamicContent" patternSyntax="ECMAScript">
                <conditions>
                    <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True" />
                </conditions>
                <action type="Rewrite" url="app.js" logRewrittenUrl="true" />
            </rule>

        </rules>
    </rewrite>

</system.webServer>
</configuration>

app.js如下:

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var hbs = require('hbs');

var index = require('./routes/index');
var compression = require('compression');
var helmet = require('helmet');

var app = express();

app.use(helmet());

// view engine setup
hbs.registerPartials(__dirname + '/views/partials');
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
hbs.registerHelper('getCurrentYear', ()=> {
    return new Date().getFullYear();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(compression()); // Compress all routes

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

app.use('/', index);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.listen(process.env.PORT || 3000);

views / partials中包含的header.hbs包含以下内容:

<link rel="stylesheet" type="text/css" href="/stylesheets/style.css">

当我在localhost:3000上运行时,没有问题。当我通过IISNode模块在IIS服务器上运行时,我在Chrome开发工具的控制台中收到以下错误消息:

  

无法加载资源:服务器响应状态为404(未找到)style.css

我觉得我错过了一些非常明显的东西。

0 个答案:

没有答案