我有以下文件夹结构:
由于我是node.js和express.js的新手,我想知道如何从控制器文件中渲染.ejs文件。目前,我的代码如下:
//控制器/ login.js
module.exports = {
getLoginPage: function (req, res) {
res.render('login-form');
}
};
//路由/ login.js
var login = require('../controllers/login');
module.exports = function(app){
app.get('/', login.getLoginPage);
};
// app.js
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'); //parses information from POST
var stylus = require('stylus');
var validator = require('express-validator');
var session = require('express-session');
var app = express();
require('./routes/login')(app);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(validator());
app.use(cookieParser());
app.use(stylus.middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({secret: 'max', saveUninitialized: false, resave: false}));
// 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 handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
在我的控制器中,如果我写res.send(" Hello");,它会打印但我希望整个.ejs文件显示在我的浏览器上。这怎么可能?
另外,如果我从路线中渲染.ejs,它会正常显示,但不能从控制器显示。
//错误
Error: Failed to lookup view "error" in views directory "C:\node\folder-name\views"
at EventEmitter.render (C:\node\folder-name\node_modules\express\lib\application.js:580:17)
at ServerResponse.render (C:\node\folder-name\node_modules\express\lib\response.js:971:7)
at C:\node\folder-name\app.js:60:7
at Layer.handle_error (C:\node\folder-name\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (C:\node\folder-name\node_modules\express\lib\router\index.js:315:13)
at C:\node\folder-name\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\node\folder-name\node_modules\express\lib\router\index.js:335:12)
at next (C:\node\folder-name\node_modules\express\lib\router\index.js:275:10)
at Layer.handle_error (C:\node\folder-name\node_modules\express\lib\router\layer.js:67:12)
at trim_prefix (C:\node\folder-name\node_modules\express\lib\router\index.js:315:13)
由于
答案 0 :(得分:2)
将您的观看路径更改为: -
app.set('views',path.join(__dirname+'/views/'));
然后在控制器中你可以简单地使用
//assuming hello.ejs is in your view folder
response.render('hello.ejs');
如果您在views文件夹中有文件夹,请使用
response.render('error/404.ejs');
答案 1 :(得分:0)
首先,您需要为视图设置渲染引擎:
app.set('view engine','ejs');
答案 2 :(得分:0)
答案 3 :(得分:0)
尝试以下方法:
>>> my = [3,4,5,8,9,6,2,4,7,5,3,1]
>>> my_min = min(my)
>>> x = [my_min]
>>> my.remove(my_min)
>>> my
[3, 4, 5, 8, 9, 6, 2, 4, 7, 5, 3]
>>> x
[1]