So I have a very simple server to test this:
import express from 'express';
const app = express();
app.set('views', './public');
const PORT = 3002;
app.get('/', (req, res) => {
res.render('index.ejs', { title: 'Page Title'});
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
What I would like to be able to do is create a link in the index.ejs to, for example, page2.ejs, page3.ejs, etc... and have express render the .ejs reached from the link without having to create a route for each page in my app.js as:
app.get('/page2.ejs', (req, res) => {
res.render('page2.ejs');
});
app.get('/page3.ejs', (req, res) => {
res.render('page3.ejs');
});
app.get('/page4.ejs', (req, res) => {
res.render('page4.ejs');
});
would seem stupidly inefficient...
So I have a very simple server to test this:
import express from 'express';
const app = express();
app.set('views', './public');
const PORT = 3002;
app.get('/', (req, res) => {
res.render('index.ejs', { title: 'Page Title'});
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
What I would like to be able to do is create a link in the index.ejs to, for example, page2.ejs, page3.ejs, etc... and have express render the .ejs reached from the link without having to create a route for each page in my app.js as:
app.get('/page2.ejs', (req, res) => {
res.render('page2.ejs');
});
app.get('/page3.ejs', (req, res) => {
res.render('page3.ejs');
});
app.get('/page4.ejs', (req, res) => {
res.render('page4.ejs');
});
would seem stupidly inefficient...
Share Improve this question asked Feb 6 at 13:50 PaulPaul 996 bronze badges3 Answers
Reset to default 2It would be:
const viewDir = path.join(__dirname, 'view');
app.get('/', ...);
app.get('/*', (req, res, next) => {
const viewPath = path.join(viewDir, req.path + '.ejs');
if (viewPath.startsWith(viewDir) && fs.existsSync(viewPath)) {
res.render(viewPath);
} else {
next();
}
});
viewPath.startsWith(viewDir)
check prevents from accessing files outside the expected location.
Well, i sort of fixed it but created another problem! The following works:
import express from 'express';
const app = express();
app.set('view engine', 'ejs');
const PORT = 3002;
app.get('/*', (req, res) => {
console.log(req.url);
// res.render('index.ejs', { title: 'Page Title'});
res.render(`.${req.url}`, { title: 'Page Title'});
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
The pages serve fine, and ejs is processed... BUUUUT: Every page request generates an error in the console:
Error: Cannot find module 'ico'
Require stack:
- C:\WDev\testSites\expressEJS\node_modules\express\lib\view.js
- C:\WDev\testSites\expressEJS\node_modules\express\lib\application.js
- C:\WDev\testSites\expressEJS\node_modules\express\lib\express.js
at Function._resolveFilename (node:internal/modules/cjs/loader:1246:15)
at Function._load (node:internal/modules/cjs/loader:1072:27)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:216:24)
at Module.require (node:internal/modules/cjs/loader:1337:12)
at require (node:internal/modules/helpers:139:16)
at new View (C:\WDev\testSites\expressEJS\node_modules\express\lib\view.js:81:14)
at Function.render (C:\WDev\testSites\expressEJS\node_modules\express\lib\application.js:587:12)
at ServerResponse.render (C:\WDev\testSites\expressEJS\node_modules\express\lib\response.js:1049:7)
at file:///C:/WDev/testSites/expressEJS/app.js:25:7
Would be really good to fix that!
Ok, solved it, needed more coffee!
import express from 'express';
const app = express();
app.set('view engine', 'ejs');
const PORT = 3002;
app.get('/*.ejs', (req, res) => {
let ejsPage = req.url;
if (ejsPage.indexOf('/') === 0) { ejsPage = ejsPage.slice(1) }
res.render(ejsPage, { title: 'Page Title'});
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
problem with my earlier solution was of course the get was trying to handle things other than .ejs (like the request from browser for favicon).