gpt4 book ai didi

node.js - 如何防止express-ejs-layouts 包装我的其他页面?

转载 作者:行者123 更新时间:2023-12-02 19:16:34 24 4
gpt4 key购买 nike

我正在寻找 Node.js 的页面布局,例如 laravel php有他们的布局模板,非常完美。我想在node.js中实现它,最后找到了这个express-ejs-layouts但其中存在一个问题,我在他们的文档中看不到布局将包装我的所有页面,特别是我的登录和注册页面,它们具有不同的页眉和页脚。我们如何防止express-ejs-layouts 包裹我的其他页面?

const express = require('express');
const router = express.Router();
const path = require('path');
const multer = require('multer');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const app = express();
const server = require('http').createServer(app);
const expressLayouts = require('express-ejs-layouts');

// Set Database Connection
const connection=mysql.createConnection({
host:'localhost',
user:'root',
password:'',
database:'project_101'
});

connection.connect(function(error){
if(!!error) console.log(error);
else console.log('Database Connected!');
});

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static('assets'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(bodyParser.urlencoded({extended: true}));
app.use(expressLayouts);

app.get('/',(req, res) => {
let sql = "SELECT * FROM uploads";
let query = connection.query(sql, (err, rows) => {
if(err) throw err;
res.render('index');
});
});

app.get('/signup', (req, res) => {
res.render('signup');
});

app.get('/signin', (req, res) => {
res.render('signin');
});

app.get('/unknown-user', (req, res) => {
res.render('unknown-user');
});

app.get('/profile', (req, res) => {
res.render('profile');
});

const port = process.env.PORT || 3000;

// Server Listening
server.listen(port, function () {
console.log('Server successfully running at: -',port);
});

最佳答案

终于找到问题的解决方案express-ejs-layouts

const express = require('express');
const expressLayouts = require('express-ejs-layouts');

app.use(expressLayouts);

You just need to declare your page as a layout and set it to false.

app.set("layout signin", false);

and render the page together with the layout.

app.get('/signin', (req, res) => {
res.render('signin', { layout: 'signin' });
});

修复⚡️

关于node.js - 如何防止express-ejs-layouts 包装我的其他页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63626198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com