无法在IISNode中运行NodeJs应用程序(Express + ejs模板)

时间:2015-03-09 12:34:46

标签: node.js express ejs iisnode

我尝试将我的NodeJs应用程序部署到IISnode几天但我仍然无法使其工作。我相信我已经安装了所有组件(URL重写模块和IISNode),只是配置错误。我使用ejs作为视图引擎,浏览器抛出错误说:

ReferenceError: C:\inetpub\wwwroot\Monito\views\layout.ejs:4
   2| <html lang="en" ng-app="app"> 
   3|   <head> 
>> 4|     <title><%= title %></title> 
   5|     <link rel="stylesheet" href="/css/bootstrap.min.css "> 
   6|     <link rel="stylesheet" href="/css/bootstrap-theme.css" > 
   7|     <link rel="stylesheet" href="/css/dashboard.css" > 

title is not defined
   at eval (eval at <anonymous> (C:\inetpub\wwwroot\Monito\node_modules\ejs\lib\ejs.js:299:12), <anonymous>:2:1943)
   at C:\inetpub\wwwroot\Monito\node_modules\ejs\lib\ejs.js:325:14
   at View.exports.renderFile [as engine] (C:\inetpub\wwwroot\Monito\node_modules\ejs\lib\ejs.js:195:31)
   at View.render (C:\inetpub\wwwroot\Monito\node_modules\express\lib\view.js:76:8)
   at Function.app.render (C:\inetpub\wwwroot\Monito\node_modules\express\lib\application.js:527:10)
   at ServerResponse.res.render (C:\inetpub\wwwroot\Monito\node_modules\express\lib\response.js:900:7)
   at C:\inetpub\wwwroot\Monito\node_modules\express-ejs-layouts\lib\express-layouts.js:80:14
   at C:\inetpub\wwwroot\Monito\node_modules\ejs\lib\ejs.js:203:5
   at process._tickCallback (node.js:442:13)

以下是我的配置:

的web.config:

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="hello.js" verb="*" modules="iisnode" />
    </handlers>
<rewrite>
      <rules>
        <rule name="myapp">
          <match url="/*" />
          <action type="Rewrite" url="hello.js" />
        </rule>
      </rules>
    </rewrite>
   </system.webServer>
</configuration>

Hello.Js:

var debug = require('debug')('Monito');
var app = require('./app');

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

var server = app.listen(app.get('port'), function() {
    debug('Express server listening on port ' + server.address().port);
});

index.js:

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var util = require('util');
var Q = require('q');

/* GET home page. */
router.get('/', function (req, res) {
res.render('index', { title: 'Dashboard', layout: 'layout.ejs'});
});

module.exports = router;

的package.json

{
  "name": "Monito",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node hello.js"
  },
  "description": "Monito",
  "author": {
    "name": "tj",
    "email": ""
  },
  "dependencies": {
    "express": "~4.9.0",
    "body-parser": "~1.8.1",
    "cookie-parser": "~1.3.3",
    "morgan": "~1.3.0",
    "serve-favicon": "~2.1.3",
    "debug": "~2.0.0",
    "jade": "~1.6.0",
    "stylus": "0.42.3"
  }
}

App.Js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');

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

var routes = require('./routes/index');
var users = require('./routes/users');
var detail = require('./routes/detail');
var log = require('./models/log_model');
var mongoose = require('mongoose');
var ejslayout = require('express-ejs-layouts');

mongoose.connect('mongodb://logger:test@127.0.0.1:27017/log');
var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(ejslayout);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/detail', detail);

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

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;

文件夹结构:

ROOT
|----routes
|    |- index.js
|    |- detail.js
|    |- users.js
|----public
|    |- css
|    |- fonts
|    |- js
|----views
|    |- error.ejs
|    |- index.ejs
|    |- layout.ejs
|----app.js
|----node_modules
|----package.json

仅供参考,我为我的开发安装了NodeJs for Visual Studio扩展,我可以通过单击&#34; Run&#34;来运行它。 VS内的按钮。任何人都可以帮忙吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

我已使用其他模板引擎atpl.js而不是ejs.js修复了此问题。

答案 1 :(得分:0)

Webconfig是正确的。

将应用程序池标识更改为LocalService,这适用于我们