简体   繁体   中英

How to set the build environment using Node.js and AngularJS?

I've created a build environment using express JS. But, I got into trouble to configure AngularJS view -- Pleas help me to fix this issue..

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"); 

Above build setup was throwing following error ::

Error : Can't find module 'html' -- I'm not sure how to configure Node.Js to refer the angular JS veiw and all the injuctors..

The problem is with this line:

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

You are trying to specify view engine with html value, which is not a view engine (expressjs tries to require html module internaly, but doesn't find it). If you need to serve just html files without templating use:

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

And move all files you want to serve to public folder.

UPD: you can actually require html file from jade

in views/index.jade

 include plain.html 

in views/plain.html

 <!DOCTYPE html> ... 

and app.js can still just render jade:

 res.render(index) 

Andrew Homeyer's answer

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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