E
E
embiid2019-10-26 14:13:07
Node.js
embiid, 2019-10-26 14:13:07

How to use multiple express layouts?

How to use multiple templates for a site?
For example, use the first template for registration and logging into the account, and the other template for the main part of the site.

const path           = require("path")
const express        = require("express")
const session        = require("express-session")
const bodyParser 	 = require("body-parser")
const expressLayouts = require("express-ejs-layouts")

const mysqlConnection = require("./models/database")

const authenticateController = require("./controllers/authenticate-controller")
const registerController	 = require("./controllers/register-controller")

const app = express()

app.set('views', __dirname + '/views/layouts');
app.set("view engine", "ejs")

app.use(express.static(path.join(__dirname, 'views')));

app.use(expressLayouts)
app.use(session({
  secret: 'secret',
  resave: true,
  saveUninitialized: true
}));
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());

app.get('/', (request, response) => {
  response.render('../partials/login')
})

app.get('/registration', (request, response) => {
  response.render('../partials/registration')
})

app.get('/feed', (request, response) =>  {
  response.render('../partials/feed', { expressLayouts: 'main.ejs' })
})

app.post('/auth/register', registerController.register)
app.post('/auth/authenticate', authenticateController.authenticate)

app.listen(3000)

And I can't figure out how the server can define another template other than layout?
5db42a40c0888534091374.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IDONTSUDO, 2019-10-27
@IDONTSUDO

How would I do it. On the example of authorization.
There is such data in the database.

email:""
password:""
role:""

role is of two types. Admin or normal user.
Admin is authorized. In response from the database, we get his role. And on the basis of this role, I would give a layout.
That is approximately.
if(res.result.role  === "admin"){
 response.render('../partials/news/admin')
}else{
 response.render('../partials/news/user')
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question