Express Js EJS Layouts Error: No default engine was specified and no extension was provided
Express Js EJS Layouts Error: No default engine was specified and no extension was provided
您好,我正在尝试在我的应用程序中使用 Express EJS 布局。一旦我尝试 app.use(expressEjsLayouts)
它就会抛出错误。没有它,应用程序工作得很好,但我想为我的模板使用 ExpressEjs-Layouts。请善待我,我对编码还很陌生 :) 我已经浏览了很多帖子,但找不到答案。这是我的 server.js
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const app = express()
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
const methodOverride = require('method-override')
const indexRouter = require("./routes/index")
app.use(express.urlencoded({ extended: false }))
app.use(flash())
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
const initializePassport = require('./passport-config')
const expressEjsLayouts = require('express-ejs-layouts')
initializePassport(
passport,
email => indexRouter.users.find(user => user.email === email),
id => indexRouter.users.find(user => user.id === id)
)
// Set Template Engine
app.use(expressEjsLayouts) // THIS SEEMS TO BE WHAT MAKES IT CRASH
app.set("layout", "layouts/layout")
app.set('view-engine', 'ejs')
app.set("views", __dirname + "/views")
// Static Files
app.use(express.static("public"))
app.use("/", indexRouter.router)
app.use(methodOverride('_method'))
app.listen(process.env.PORT || 3000)
这是错误:
Error: No default engine was specified and no extension was provided.
at new View (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\view.js:61:11)
at Function.render (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\response.js:1012:7)
at ServerResponse.res.render [as __render] (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express-flash\lib\express-flash.js:29:16)
at C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20
at tryHandleCache (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\ejs\lib\ejs.js:260:5)
at View.exports.renderFile [as engine] (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\ejs\lib\ejs.js:485:10)
at View.render (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\application.js:592:3)
您需要告诉 Express 您正在使用哪个视图引擎。
您可以使用 app.set()
方法来完成。
它需要两个参数
1.what 你正在设置。在这种情况下是一个视图引擎
2.the 您希望使用的视图引擎类型
例如app.set('view engine', 'ejs')
;
或 app.set('view engine', 'pug')
;
这是文档https://expressjs.com/en/guide/using-template-engines.html
您好,我正在尝试在我的应用程序中使用 Express EJS 布局。一旦我尝试 app.use(expressEjsLayouts)
它就会抛出错误。没有它,应用程序工作得很好,但我想为我的模板使用 ExpressEjs-Layouts。请善待我,我对编码还很陌生 :) 我已经浏览了很多帖子,但找不到答案。这是我的 server.js
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const app = express()
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
const methodOverride = require('method-override')
const indexRouter = require("./routes/index")
app.use(express.urlencoded({ extended: false }))
app.use(flash())
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
const initializePassport = require('./passport-config')
const expressEjsLayouts = require('express-ejs-layouts')
initializePassport(
passport,
email => indexRouter.users.find(user => user.email === email),
id => indexRouter.users.find(user => user.id === id)
)
// Set Template Engine
app.use(expressEjsLayouts) // THIS SEEMS TO BE WHAT MAKES IT CRASH
app.set("layout", "layouts/layout")
app.set('view-engine', 'ejs')
app.set("views", __dirname + "/views")
// Static Files
app.use(express.static("public"))
app.use("/", indexRouter.router)
app.use(methodOverride('_method'))
app.listen(process.env.PORT || 3000)
这是错误:
Error: No default engine was specified and no extension was provided.
at new View (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\view.js:61:11)
at Function.render (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\response.js:1012:7)
at ServerResponse.res.render [as __render] (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express-flash\lib\express-flash.js:29:16)
at C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20
at tryHandleCache (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\ejs\lib\ejs.js:260:5)
at View.exports.renderFile [as engine] (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\ejs\lib\ejs.js:485:10)
at View.render (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\admin\Documents\Alles mit JS\gefahrengueterproject\node_modules\express\lib\application.js:592:3)
您需要告诉 Express 您正在使用哪个视图引擎。
您可以使用 app.set()
方法来完成。
它需要两个参数
1.what 你正在设置。在这种情况下是一个视图引擎
2.the 您希望使用的视图引擎类型
例如app.set('view engine', 'ejs')
;
或 app.set('view engine', 'pug')
;
这是文档https://expressjs.com/en/guide/using-template-engines.html