Templating libraries

Embedded Javascript templates (ejs)

Link: https://ejs.co/

ΠžΡ‡Π΅Π½ΡŒ популярный ΡˆΠ°Π±Π»ΠΎΠ½ΠΈΠ·Π°Ρ‚ΠΎΡ€ Π² NodeJS ΠΌΠΈΡ€Π΅

Как ΠΎΠ½ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ с Express

$ tree
-> app.js
-> views
----> index.ejs

$ npm i express ejs
$ node app.js
App listening on port 3000

Π’ views Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΡŽ Π·Π°ΠΊΠΈΠ΄Ρ‹Π²Π°Π΅ΠΌ наши ΡˆΠ°Π±Π»ΠΎΠ½Ρ‹. Доступ ΠΊ Π½ΠΈΠΌ Ρ‡Π΅Ρ€Π΅Π· Π²Ρ‹Π·ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ res.render().

app.js:

// Setup app
const express = require("express");
const app  = express();
const port = 3000;

// Select ejs templating library
app.set('view engine', 'ejs');

// Routes
app.get("/", (req, res) => {
    res.render("index");
})

// Start app
app.listen(port, () => {
    console.log(`App listening on port ${port}`)
})

Last updated