ExpressJS static dynamically - node.js

I'd like to serve /mbti/:lang/ with static files from directory ./mbti/:lang/.
That is:
/mbti/en/ -> ./mbti/en/index.html
/mbti/en/some.json -> ./mbti/en/some.json
I found static, but seems like it can't do this:
app.use('/mbti/:lang/', express.static(__dirname + '/mbti/' + :lang));
How can I implement this? thanks.

You shouldn't need to do that, just make /mbti/ a static route and anything below that root will be served as static
express.static(__dirname + '/mbti');

Related

codeigniter4 - How do I shorten the path to the view folder for modules?

Based on a video with Codeigniter4, I created the Modules folder on the ROOTPATH and the Controllers, Views..etc folders in the Modules folder. It works fine, but when I want to call my view file inside the module
<?php
namespace Modules\Giris\Controllers;
use App\Controllers\BaseController;
class IndexController extends BaseController
{
public function index(){
return view('Modules\Giris\Views\index');
}
}
I need to specify a very long path like How can I make it just like view('index') and call the file from the Views folder in that module if I write it in a module? I don't want to write "Modules\Login\Views" in short is this possible?
Thanks in advance for all the kind replies.
Because view requires a string you couldn't provide a namespaced reference as you might think you should. Furthermore the code adds a .php extension and the "view path" (defined in your config\paths file) as part of its process (see system/View/View.php and render()). Therefore without modifying Codeigniter (which could be done but would affect all your code) the easiest way is to simply declare a public property or constant and make reference to that instead. Also helps if you need to change the path at any point.
I.e. protected $path = 'Modules\Giris\Views\' and then view($this->path.'index'); is probably the easiest way.

Nunjucks rendering file paths relative to route being called and overriding express.static setting

Using Nunjucks with Node
Trying to figure out a graceful solution to the following problem. With a directory tree sorda like this:
app_dir
--app.js
--public
----stylesheets
------mystyles.css
--views
----page.html
----templates
------page_template.html
Have static files like CSS inside my public directory
app.use(express.static(path.join(__dirname, 'public')));
Have the root directory of Nunjucks configured as views
nunjucks.configure('views', {
autoescape: true,
express : app,
watch: true
});
When I am referencing a css file from within page_template.html, nunjucks (I think) automagically creates a relative path based on the route and overrides the static behavior.
For example, when I use /stylesheets/mystyles.css path on page_template.html but call the file that extends it using
/:publication/:page path, the rendered html is /:publication/:page/stylesheets/mystyle.css
I can always write a quick hack that creates relative paths to CSS and other resources based on the route but that doesn't feel like a particularly graceful solution :( Any help much appreciated.
When I am referencing a css file from within page_template.html, nunjucks (I think) automagically creates a relative path based on the route and overrides the static behavior.
I think it's mistake. Nunjucks don't generate any path.
In template from any folder (view, view/templates, etc) you must specify filename considering that public dir is root, e.g.
/stylesheets/mystyles.css for %app%/public/stylesheets/mystyles.css.
I use subfolders in view for grouping templates, e.g. /macros (stored macros), /tools (stored additional pages for my app). Also you can use it to router, e.g. /user/view.html, user/add.html...

How to set a variable layout in Jade?

I want to set a customisable layout path in Jade.
I get the path from my app and put it in Express in res.locals._layout like it (app.js is under /lib):
app.set('views', __dirname + '/../views');
app.set('view engine', 'jade');
res.locals._layout = layout_path;
Then I try to pass it to extends in my view like it:
extends _layout
I also tested extends #{_layout}, with also bad results...
Here is the error I get for the last one:
ENOENT, no such file or directory '/root_path/views/#{_layout}.jade'
The doc is not verbose on such a point.
I don't think what you're trying to do is supported by jade. The extends is resolved when the template is compiled, before any res.locals state can be applied. But there are some workarounds mentioned in this thread.
https://github.com/jadejs/jade/issues/520

Static route to uploaded files

I need to access a file in the static folder of Yesod, which gets uploaded there at runtime, so it does not have an identifier.
Is there any way to construct a static route to such file?
You can use the StaticRoute constructor directly, along the lines of:
StaticRoute ["somefile.txt"] []

CoffeeScript compiling express.static __dirname + '/public' wrong

I am pretty new to CoffeeScript. I am trying to create Node.js application using the Express.js framework. And in my express app configuration I have this line of code that is compiling wrong:
app.use express.static path + '/public'
it is compiling to this:
app.use(express["static"](path + '/public'));
when I need to be this:
app.use(express.static(path + '/public'));
Does anyone know why this happening and how to fix this? It is causing my public folder to unaccessible.
I am using CoffeeScript 1.3.1
static could be a reserved word in future versions of javascript/ecmascript. Just like top now. So using it as a variable name could cause errors somewhere.
That's why coffee is trying to avoid it.
But they are equivalent, so try to find errors somewhere else.
They're equivalent, don't worry about it.
Express framework using 'serve-static' module for export static method:
exports.static = require('serve-static');
You may try solve your problem like this:
app.use '/static', require('serve-static')(__dirname + '/static')
or override static method in your module.

Resources