如何使用Node.js和AngularJS设置构建环境?

时间:2015-05-19 18:33:10

标签: javascript angularjs node.js express

我使用express JS创建了一个构建环境。但是,我在配置AngularJS视图时遇到了麻烦 - 请帮我解决这个问题..

1

var express = require('express');
var http = require('http');
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 app = express();
var httpServer = http.createServer(app);
// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
//res.render('dist/index.html');
//app.engine('html', require('ejs').renderFile);


//Trying to setup my view over here -- dist folder is holding the index.html file 

app.set('views', __dirname + '/dist');
app.set('view engine', 'html');


// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/dist/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static('./dist'));

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

// 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: {}
  });
});

httpServer.listen('8080');
console.log("server is started");

以上构建设置引发了以下错误::

错误:无法找到模块'html' - 我不知道如何配置Node.Js以引用角度JS视图和所有伤害者..

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

app.set('view engine', 'html');

您正在尝试使用html值指定视图引擎,这不是视图引擎(expressjs尝试内部需要html模块,但找不到它)。如果您只需提供html文件而不进行模板化使用:

app.use(express.static(__dirname + '/public'));

将您要提供的所有文件移至公用文件夹。

UPD:您实际上可以要求来自jade的html文件

  在views / index.jade中

include plain.html
     

in views / plain.html

<!DOCTYPE html>
...
     

和app.js仍然只能渲染玉:

res.render(index)
     

Python documentation

参考文献: