middleware on res.render() when using a lot of routes









up vote
0
down vote

favorite












I've created a webpage to use it locally. I have a ton of routes like the ones shown below -- 31 .ejs files and 3 .html files. (They are all in the same "views" folder).



//app.js - using node and express
app.get('/page1', function(req, res)
res.render('page1');
);
app.get('/page2', function(req, res)
res.sendFile('views/page2.html', root: __dirname );
);


I use an app.get for each and every one of these files. I've had a feeling it wasn't DRY code, and so now I'm trying to figure out a more elegant and optimal way to achieve the same result.



  • I know that many res.sendFile(); could be replaced with a single express.static() middleware statement. I usually use express.static() on a "public" folder which I use to save all my css files -- like this app.use(express.static(path.join(__dirname, 'public')));. But I still don't see how I could use this to simplify all my res.sendFile().


  • As for the many res.render(); routes, I know that if I don't pass any customized data I could probably replace them with a single middleware that handles either a whole directory of template files (and their corresponding routes) or a list of files. I just don't know how I would do that.




Any help is very much appreciated, thanks!!




[UPDATE]


  • richie
    • node_modules
    • public

      • css files, images, etc
    • views

      • partials
        • all partial files
      • programmingPublic
        • all ejs files from a same topic
      • other files (html & other ejs)
    • appjs

    • packagejson
    • package-lockjson






const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

const app = express();

// Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended: false));

// engine
app.set("view engine", "ejs");

// Set static path
app.use(express.static(path.join(__dirname, 'public')));

const fs = require('fs');

function renderStatic(dir)
return function(req, res, next)
let target = path.join(dir, req.path);
fs.access(target, fs.constants.R_OK, function(err)
if (err)
// file not found, just move on
next();
else
res.render(target);

);



app.use(renderStatic(path.join(__dirname, "views/programmingPublic")));


Below is the format of my side-menu: (all these files are inside "programmingPublic" folder)



<a href="/programming" class="title">Programming</a>
<li><a href="/c">C</a></li>
<li><a href="/cpp">C++</a></li>
<li><a href="/python">Python</a></li>
<li><a href="/javascript">JavaScript</a></li>
<li><a href="/php">PHP</a></li>









share|improve this question























  • In this directory hierarchy, where is __dirname? And, when you have a link like /javascript in your side-menu, what file are you hoping for that to display? And, where is it?
    – jfriend00
    Nov 10 at 1:52











  • I thought __dirname was the "richie" folder since it's the root of all files & folders. /javascript is the name of the page -- I save info about the language on it.
    – richie
    Nov 10 at 1:57











  • all these files (programming, c, cpp, python, javascript, php) are inside the programmingPublic folder
    – richie
    Nov 10 at 1:58










  • Are you trying to render files that have no file extension?
    – jfriend00
    Nov 10 at 2:01










  • they are all .ejs files. I use app.set("view engine", "ejs"); on the app.js file (like I showed above) and it always works.
    – richie
    Nov 10 at 2:04















up vote
0
down vote

favorite












I've created a webpage to use it locally. I have a ton of routes like the ones shown below -- 31 .ejs files and 3 .html files. (They are all in the same "views" folder).



//app.js - using node and express
app.get('/page1', function(req, res)
res.render('page1');
);
app.get('/page2', function(req, res)
res.sendFile('views/page2.html', root: __dirname );
);


I use an app.get for each and every one of these files. I've had a feeling it wasn't DRY code, and so now I'm trying to figure out a more elegant and optimal way to achieve the same result.



  • I know that many res.sendFile(); could be replaced with a single express.static() middleware statement. I usually use express.static() on a "public" folder which I use to save all my css files -- like this app.use(express.static(path.join(__dirname, 'public')));. But I still don't see how I could use this to simplify all my res.sendFile().


  • As for the many res.render(); routes, I know that if I don't pass any customized data I could probably replace them with a single middleware that handles either a whole directory of template files (and their corresponding routes) or a list of files. I just don't know how I would do that.




Any help is very much appreciated, thanks!!




[UPDATE]


  • richie
    • node_modules
    • public

      • css files, images, etc
    • views

      • partials
        • all partial files
      • programmingPublic
        • all ejs files from a same topic
      • other files (html & other ejs)
    • appjs

    • packagejson
    • package-lockjson






const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

const app = express();

// Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended: false));

// engine
app.set("view engine", "ejs");

// Set static path
app.use(express.static(path.join(__dirname, 'public')));

const fs = require('fs');

function renderStatic(dir)
return function(req, res, next)
let target = path.join(dir, req.path);
fs.access(target, fs.constants.R_OK, function(err)
if (err)
// file not found, just move on
next();
else
res.render(target);

);



app.use(renderStatic(path.join(__dirname, "views/programmingPublic")));


Below is the format of my side-menu: (all these files are inside "programmingPublic" folder)



<a href="/programming" class="title">Programming</a>
<li><a href="/c">C</a></li>
<li><a href="/cpp">C++</a></li>
<li><a href="/python">Python</a></li>
<li><a href="/javascript">JavaScript</a></li>
<li><a href="/php">PHP</a></li>









share|improve this question























  • In this directory hierarchy, where is __dirname? And, when you have a link like /javascript in your side-menu, what file are you hoping for that to display? And, where is it?
    – jfriend00
    Nov 10 at 1:52











  • I thought __dirname was the "richie" folder since it's the root of all files & folders. /javascript is the name of the page -- I save info about the language on it.
    – richie
    Nov 10 at 1:57











  • all these files (programming, c, cpp, python, javascript, php) are inside the programmingPublic folder
    – richie
    Nov 10 at 1:58










  • Are you trying to render files that have no file extension?
    – jfriend00
    Nov 10 at 2:01










  • they are all .ejs files. I use app.set("view engine", "ejs"); on the app.js file (like I showed above) and it always works.
    – richie
    Nov 10 at 2:04













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've created a webpage to use it locally. I have a ton of routes like the ones shown below -- 31 .ejs files and 3 .html files. (They are all in the same "views" folder).



//app.js - using node and express
app.get('/page1', function(req, res)
res.render('page1');
);
app.get('/page2', function(req, res)
res.sendFile('views/page2.html', root: __dirname );
);


I use an app.get for each and every one of these files. I've had a feeling it wasn't DRY code, and so now I'm trying to figure out a more elegant and optimal way to achieve the same result.



  • I know that many res.sendFile(); could be replaced with a single express.static() middleware statement. I usually use express.static() on a "public" folder which I use to save all my css files -- like this app.use(express.static(path.join(__dirname, 'public')));. But I still don't see how I could use this to simplify all my res.sendFile().


  • As for the many res.render(); routes, I know that if I don't pass any customized data I could probably replace them with a single middleware that handles either a whole directory of template files (and their corresponding routes) or a list of files. I just don't know how I would do that.




Any help is very much appreciated, thanks!!




[UPDATE]


  • richie
    • node_modules
    • public

      • css files, images, etc
    • views

      • partials
        • all partial files
      • programmingPublic
        • all ejs files from a same topic
      • other files (html & other ejs)
    • appjs

    • packagejson
    • package-lockjson






const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

const app = express();

// Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended: false));

// engine
app.set("view engine", "ejs");

// Set static path
app.use(express.static(path.join(__dirname, 'public')));

const fs = require('fs');

function renderStatic(dir)
return function(req, res, next)
let target = path.join(dir, req.path);
fs.access(target, fs.constants.R_OK, function(err)
if (err)
// file not found, just move on
next();
else
res.render(target);

);



app.use(renderStatic(path.join(__dirname, "views/programmingPublic")));


Below is the format of my side-menu: (all these files are inside "programmingPublic" folder)



<a href="/programming" class="title">Programming</a>
<li><a href="/c">C</a></li>
<li><a href="/cpp">C++</a></li>
<li><a href="/python">Python</a></li>
<li><a href="/javascript">JavaScript</a></li>
<li><a href="/php">PHP</a></li>









share|improve this question















I've created a webpage to use it locally. I have a ton of routes like the ones shown below -- 31 .ejs files and 3 .html files. (They are all in the same "views" folder).



//app.js - using node and express
app.get('/page1', function(req, res)
res.render('page1');
);
app.get('/page2', function(req, res)
res.sendFile('views/page2.html', root: __dirname );
);


I use an app.get for each and every one of these files. I've had a feeling it wasn't DRY code, and so now I'm trying to figure out a more elegant and optimal way to achieve the same result.



  • I know that many res.sendFile(); could be replaced with a single express.static() middleware statement. I usually use express.static() on a "public" folder which I use to save all my css files -- like this app.use(express.static(path.join(__dirname, 'public')));. But I still don't see how I could use this to simplify all my res.sendFile().


  • As for the many res.render(); routes, I know that if I don't pass any customized data I could probably replace them with a single middleware that handles either a whole directory of template files (and their corresponding routes) or a list of files. I just don't know how I would do that.




Any help is very much appreciated, thanks!!




[UPDATE]


  • richie
    • node_modules
    • public

      • css files, images, etc
    • views

      • partials
        • all partial files
      • programmingPublic
        • all ejs files from a same topic
      • other files (html & other ejs)
    • appjs

    • packagejson
    • package-lockjson






const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

const app = express();

// Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended: false));

// engine
app.set("view engine", "ejs");

// Set static path
app.use(express.static(path.join(__dirname, 'public')));

const fs = require('fs');

function renderStatic(dir)
return function(req, res, next)
let target = path.join(dir, req.path);
fs.access(target, fs.constants.R_OK, function(err)
if (err)
// file not found, just move on
next();
else
res.render(target);

);



app.use(renderStatic(path.join(__dirname, "views/programmingPublic")));


Below is the format of my side-menu: (all these files are inside "programmingPublic" folder)



<a href="/programming" class="title">Programming</a>
<li><a href="/c">C</a></li>
<li><a href="/cpp">C++</a></li>
<li><a href="/python">Python</a></li>
<li><a href="/javascript">JavaScript</a></li>
<li><a href="/php">PHP</a></li>






javascript node.js express






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 1:36

























asked Nov 9 at 22:01









richie

265




265











  • In this directory hierarchy, where is __dirname? And, when you have a link like /javascript in your side-menu, what file are you hoping for that to display? And, where is it?
    – jfriend00
    Nov 10 at 1:52











  • I thought __dirname was the "richie" folder since it's the root of all files & folders. /javascript is the name of the page -- I save info about the language on it.
    – richie
    Nov 10 at 1:57











  • all these files (programming, c, cpp, python, javascript, php) are inside the programmingPublic folder
    – richie
    Nov 10 at 1:58










  • Are you trying to render files that have no file extension?
    – jfriend00
    Nov 10 at 2:01










  • they are all .ejs files. I use app.set("view engine", "ejs"); on the app.js file (like I showed above) and it always works.
    – richie
    Nov 10 at 2:04

















  • In this directory hierarchy, where is __dirname? And, when you have a link like /javascript in your side-menu, what file are you hoping for that to display? And, where is it?
    – jfriend00
    Nov 10 at 1:52











  • I thought __dirname was the "richie" folder since it's the root of all files & folders. /javascript is the name of the page -- I save info about the language on it.
    – richie
    Nov 10 at 1:57











  • all these files (programming, c, cpp, python, javascript, php) are inside the programmingPublic folder
    – richie
    Nov 10 at 1:58










  • Are you trying to render files that have no file extension?
    – jfriend00
    Nov 10 at 2:01










  • they are all .ejs files. I use app.set("view engine", "ejs"); on the app.js file (like I showed above) and it always works.
    – richie
    Nov 10 at 2:04
















In this directory hierarchy, where is __dirname? And, when you have a link like /javascript in your side-menu, what file are you hoping for that to display? And, where is it?
– jfriend00
Nov 10 at 1:52





In this directory hierarchy, where is __dirname? And, when you have a link like /javascript in your side-menu, what file are you hoping for that to display? And, where is it?
– jfriend00
Nov 10 at 1:52













I thought __dirname was the "richie" folder since it's the root of all files & folders. /javascript is the name of the page -- I save info about the language on it.
– richie
Nov 10 at 1:57





I thought __dirname was the "richie" folder since it's the root of all files & folders. /javascript is the name of the page -- I save info about the language on it.
– richie
Nov 10 at 1:57













all these files (programming, c, cpp, python, javascript, php) are inside the programmingPublic folder
– richie
Nov 10 at 1:58




all these files (programming, c, cpp, python, javascript, php) are inside the programmingPublic folder
– richie
Nov 10 at 1:58












Are you trying to render files that have no file extension?
– jfriend00
Nov 10 at 2:01




Are you trying to render files that have no file extension?
– jfriend00
Nov 10 at 2:01












they are all .ejs files. I use app.set("view engine", "ejs"); on the app.js file (like I showed above) and it always works.
– richie
Nov 10 at 2:04





they are all .ejs files. I use app.set("view engine", "ejs"); on the app.js file (like I showed above) and it always works.
– richie
Nov 10 at 2:04













1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










If you have a bunch of pages that need to call res.render(), but aren't passing custom options to each render, then you could isolate all those templates in their own directory and then use some middleware like this:



const path = require('path');
const fs = require('fs');

function renderStatic(dir, options)
const regex = /^.

app.use(renderStatic(path.join(__dirname, "renderPublic"), ext: ".ejs"));


Note, you must isolate these template files in their own directory so that other files are not found there.



For safety completeness, this code also needs to filter out . and .. items in the path like express.static() does to prevent an attacker from going up your directory hierarchy to get access to other files than those in the render static directory.




Then, for the routes you are using res.sendFile() and no other logic, just isolate those HTML files in their own directory and point express.static() at that directory. Then, the express.static() middleware will find a matching HTML file in that directory and do res.sendFile() for you automatically, exactly the same as it does for your CSS files.






share|improve this answer


















  • 1




    @richie - No. Don't add renderPublic to the HTML page at all. That's local to the server only. That's where you PUT the template files on your server - not anything to do with the public URL for that. Do you understand the difference between the public URL (that the server gets a request for) and the directory that the middleware looks in for a matching file?
    – jfriend00
    Nov 10 at 0:54






  • 1




    @richie - Using the exact code in my answer, you would create a sub-directory under __dirname (where you app is located) and put the template files in there. But, you can obviously put them wherever you want and just pass the right path to them to the renderStatic() middleware so it knows where to look for matches.
    – jfriend00
    Nov 10 at 0:57







  • 1




    @richie - I modified my code so you don't need the .ejs on the URLs.
    – jfriend00
    Nov 10 at 6:00






  • 1




    @richie - Notice that my latest version also has the security fix that prevents paths that walk UP the hierarchy with things like ./ in the path.
    – jfriend00
    Nov 10 at 6:22






  • 1




    @richie - By using an URL like this: /../../../app.js. This would be capable of looking on your hard disk ABOVE the renderPublic directory and fetching those files which is not what you want anyone to be able to do. That's why we disable it if there are any leading dots in any path segment.
    – jfriend00
    Nov 10 at 6:29











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233806%2fmiddleware-on-res-render-when-using-a-lot-of-routes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










If you have a bunch of pages that need to call res.render(), but aren't passing custom options to each render, then you could isolate all those templates in their own directory and then use some middleware like this:



const path = require('path');
const fs = require('fs');

function renderStatic(dir, options)
const regex = /^.

app.use(renderStatic(path.join(__dirname, "renderPublic"), ext: ".ejs"));


Note, you must isolate these template files in their own directory so that other files are not found there.



For safety completeness, this code also needs to filter out . and .. items in the path like express.static() does to prevent an attacker from going up your directory hierarchy to get access to other files than those in the render static directory.




Then, for the routes you are using res.sendFile() and no other logic, just isolate those HTML files in their own directory and point express.static() at that directory. Then, the express.static() middleware will find a matching HTML file in that directory and do res.sendFile() for you automatically, exactly the same as it does for your CSS files.






share|improve this answer


















  • 1




    @richie - No. Don't add renderPublic to the HTML page at all. That's local to the server only. That's where you PUT the template files on your server - not anything to do with the public URL for that. Do you understand the difference between the public URL (that the server gets a request for) and the directory that the middleware looks in for a matching file?
    – jfriend00
    Nov 10 at 0:54






  • 1




    @richie - Using the exact code in my answer, you would create a sub-directory under __dirname (where you app is located) and put the template files in there. But, you can obviously put them wherever you want and just pass the right path to them to the renderStatic() middleware so it knows where to look for matches.
    – jfriend00
    Nov 10 at 0:57







  • 1




    @richie - I modified my code so you don't need the .ejs on the URLs.
    – jfriend00
    Nov 10 at 6:00






  • 1




    @richie - Notice that my latest version also has the security fix that prevents paths that walk UP the hierarchy with things like ./ in the path.
    – jfriend00
    Nov 10 at 6:22






  • 1




    @richie - By using an URL like this: /../../../app.js. This would be capable of looking on your hard disk ABOVE the renderPublic directory and fetching those files which is not what you want anyone to be able to do. That's why we disable it if there are any leading dots in any path segment.
    – jfriend00
    Nov 10 at 6:29















up vote
1
down vote



accepted










If you have a bunch of pages that need to call res.render(), but aren't passing custom options to each render, then you could isolate all those templates in their own directory and then use some middleware like this:



const path = require('path');
const fs = require('fs');

function renderStatic(dir, options)
const regex = /^.

app.use(renderStatic(path.join(__dirname, "renderPublic"), ext: ".ejs"));


Note, you must isolate these template files in their own directory so that other files are not found there.



For safety completeness, this code also needs to filter out . and .. items in the path like express.static() does to prevent an attacker from going up your directory hierarchy to get access to other files than those in the render static directory.




Then, for the routes you are using res.sendFile() and no other logic, just isolate those HTML files in their own directory and point express.static() at that directory. Then, the express.static() middleware will find a matching HTML file in that directory and do res.sendFile() for you automatically, exactly the same as it does for your CSS files.






share|improve this answer


















  • 1




    @richie - No. Don't add renderPublic to the HTML page at all. That's local to the server only. That's where you PUT the template files on your server - not anything to do with the public URL for that. Do you understand the difference between the public URL (that the server gets a request for) and the directory that the middleware looks in for a matching file?
    – jfriend00
    Nov 10 at 0:54






  • 1




    @richie - Using the exact code in my answer, you would create a sub-directory under __dirname (where you app is located) and put the template files in there. But, you can obviously put them wherever you want and just pass the right path to them to the renderStatic() middleware so it knows where to look for matches.
    – jfriend00
    Nov 10 at 0:57







  • 1




    @richie - I modified my code so you don't need the .ejs on the URLs.
    – jfriend00
    Nov 10 at 6:00






  • 1




    @richie - Notice that my latest version also has the security fix that prevents paths that walk UP the hierarchy with things like ./ in the path.
    – jfriend00
    Nov 10 at 6:22






  • 1




    @richie - By using an URL like this: /../../../app.js. This would be capable of looking on your hard disk ABOVE the renderPublic directory and fetching those files which is not what you want anyone to be able to do. That's why we disable it if there are any leading dots in any path segment.
    – jfriend00
    Nov 10 at 6:29













up vote
1
down vote



accepted







up vote
1
down vote



accepted






If you have a bunch of pages that need to call res.render(), but aren't passing custom options to each render, then you could isolate all those templates in their own directory and then use some middleware like this:



const path = require('path');
const fs = require('fs');

function renderStatic(dir, options)
const regex = /^.

app.use(renderStatic(path.join(__dirname, "renderPublic"), ext: ".ejs"));


Note, you must isolate these template files in their own directory so that other files are not found there.



For safety completeness, this code also needs to filter out . and .. items in the path like express.static() does to prevent an attacker from going up your directory hierarchy to get access to other files than those in the render static directory.




Then, for the routes you are using res.sendFile() and no other logic, just isolate those HTML files in their own directory and point express.static() at that directory. Then, the express.static() middleware will find a matching HTML file in that directory and do res.sendFile() for you automatically, exactly the same as it does for your CSS files.






share|improve this answer














If you have a bunch of pages that need to call res.render(), but aren't passing custom options to each render, then you could isolate all those templates in their own directory and then use some middleware like this:



const path = require('path');
const fs = require('fs');

function renderStatic(dir, options)
const regex = /^.

app.use(renderStatic(path.join(__dirname, "renderPublic"), ext: ".ejs"));


Note, you must isolate these template files in their own directory so that other files are not found there.



For safety completeness, this code also needs to filter out . and .. items in the path like express.static() does to prevent an attacker from going up your directory hierarchy to get access to other files than those in the render static directory.




Then, for the routes you are using res.sendFile() and no other logic, just isolate those HTML files in their own directory and point express.static() at that directory. Then, the express.static() middleware will find a matching HTML file in that directory and do res.sendFile() for you automatically, exactly the same as it does for your CSS files.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 6:19

























answered Nov 9 at 22:56









jfriend00

422k51535583




422k51535583







  • 1




    @richie - No. Don't add renderPublic to the HTML page at all. That's local to the server only. That's where you PUT the template files on your server - not anything to do with the public URL for that. Do you understand the difference between the public URL (that the server gets a request for) and the directory that the middleware looks in for a matching file?
    – jfriend00
    Nov 10 at 0:54






  • 1




    @richie - Using the exact code in my answer, you would create a sub-directory under __dirname (where you app is located) and put the template files in there. But, you can obviously put them wherever you want and just pass the right path to them to the renderStatic() middleware so it knows where to look for matches.
    – jfriend00
    Nov 10 at 0:57







  • 1




    @richie - I modified my code so you don't need the .ejs on the URLs.
    – jfriend00
    Nov 10 at 6:00






  • 1




    @richie - Notice that my latest version also has the security fix that prevents paths that walk UP the hierarchy with things like ./ in the path.
    – jfriend00
    Nov 10 at 6:22






  • 1




    @richie - By using an URL like this: /../../../app.js. This would be capable of looking on your hard disk ABOVE the renderPublic directory and fetching those files which is not what you want anyone to be able to do. That's why we disable it if there are any leading dots in any path segment.
    – jfriend00
    Nov 10 at 6:29













  • 1




    @richie - No. Don't add renderPublic to the HTML page at all. That's local to the server only. That's where you PUT the template files on your server - not anything to do with the public URL for that. Do you understand the difference between the public URL (that the server gets a request for) and the directory that the middleware looks in for a matching file?
    – jfriend00
    Nov 10 at 0:54






  • 1




    @richie - Using the exact code in my answer, you would create a sub-directory under __dirname (where you app is located) and put the template files in there. But, you can obviously put them wherever you want and just pass the right path to them to the renderStatic() middleware so it knows where to look for matches.
    – jfriend00
    Nov 10 at 0:57







  • 1




    @richie - I modified my code so you don't need the .ejs on the URLs.
    – jfriend00
    Nov 10 at 6:00






  • 1




    @richie - Notice that my latest version also has the security fix that prevents paths that walk UP the hierarchy with things like ./ in the path.
    – jfriend00
    Nov 10 at 6:22






  • 1




    @richie - By using an URL like this: /../../../app.js. This would be capable of looking on your hard disk ABOVE the renderPublic directory and fetching those files which is not what you want anyone to be able to do. That's why we disable it if there are any leading dots in any path segment.
    – jfriend00
    Nov 10 at 6:29








1




1




@richie - No. Don't add renderPublic to the HTML page at all. That's local to the server only. That's where you PUT the template files on your server - not anything to do with the public URL for that. Do you understand the difference between the public URL (that the server gets a request for) and the directory that the middleware looks in for a matching file?
– jfriend00
Nov 10 at 0:54




@richie - No. Don't add renderPublic to the HTML page at all. That's local to the server only. That's where you PUT the template files on your server - not anything to do with the public URL for that. Do you understand the difference between the public URL (that the server gets a request for) and the directory that the middleware looks in for a matching file?
– jfriend00
Nov 10 at 0:54




1




1




@richie - Using the exact code in my answer, you would create a sub-directory under __dirname (where you app is located) and put the template files in there. But, you can obviously put them wherever you want and just pass the right path to them to the renderStatic() middleware so it knows where to look for matches.
– jfriend00
Nov 10 at 0:57





@richie - Using the exact code in my answer, you would create a sub-directory under __dirname (where you app is located) and put the template files in there. But, you can obviously put them wherever you want and just pass the right path to them to the renderStatic() middleware so it knows where to look for matches.
– jfriend00
Nov 10 at 0:57





1




1




@richie - I modified my code so you don't need the .ejs on the URLs.
– jfriend00
Nov 10 at 6:00




@richie - I modified my code so you don't need the .ejs on the URLs.
– jfriend00
Nov 10 at 6:00




1




1




@richie - Notice that my latest version also has the security fix that prevents paths that walk UP the hierarchy with things like ./ in the path.
– jfriend00
Nov 10 at 6:22




@richie - Notice that my latest version also has the security fix that prevents paths that walk UP the hierarchy with things like ./ in the path.
– jfriend00
Nov 10 at 6:22




1




1




@richie - By using an URL like this: /../../../app.js. This would be capable of looking on your hard disk ABOVE the renderPublic directory and fetching those files which is not what you want anyone to be able to do. That's why we disable it if there are any leading dots in any path segment.
– jfriend00
Nov 10 at 6:29





@richie - By using an URL like this: /../../../app.js. This would be capable of looking on your hard disk ABOVE the renderPublic directory and fetching those files which is not what you want anyone to be able to do. That's why we disable it if there are any leading dots in any path segment.
– jfriend00
Nov 10 at 6:29


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233806%2fmiddleware-on-res-render-when-using-a-lot-of-routes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus