How to Serving static files with express if it is inside a subdirectory - express

I am having trouble with serving a html file which is inside a subdirectory inside public folder.
My directory structure looks like:
public/
HTML/
index.html
index.js
I want to serve the index.html for the root route.
This doesn't work:
app.use('/',express.static(path.join(__dirname,'public')));

According to the docs, this should be enough:
app.use(express.static('public'))
It also gives you such example
http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html
It doesn't go much further other than explaining how you can set up multiple public directories. Anyway, it has worked for me in my projects.
Perhaps you need to state directly app.use(express.static('html')) or app.use(express.static('public/html')). Let me know what works for you.

Related

How to referemce favicon in index.html from /source

Can someone please tell me, if it is possible to use the favicon.ico file that is located in src/assets/img/favicon.ico? I tried to change the href but it won't work for me. The one in the public will be deleted. How could I reference it in my index.html?
Thank you
BASE_URL returns public folder, you can use cdn or do not delete inside public folder because if you build this code there won't be src folder.

Serving express files from base directory

How do would I serve static files from my base directory? Would it just be a / or would I have to include the name of the base directory, which in this case would be Scanning
app.use(express.static(join(__dirname, '/')));
Pretty close, just a few adjustments!
app.use(express.static(`${__dirname}/Scanning`))
You need to use or construct the actual full path to the base directory. You don't show your actual directory structure and where the desired directory is relative to the directory that your code is running from.
If you wanted express.static() to serve from the Scanning directory which is a sub-directory of the directory you code is located in, you would do this:
app.use(express.static(path.join(__dirname, 'Scanning')));
Or, if Scanning is a sibling of __dirname, then it would be this:
app.use(express.static(path.join(__dirname, '../Scanning')));
You should never be serving files with express.static() directly from __dirname because that would allow your server to serve up your actual source files (and sometimes things like credentials).

How to generate txt & xml files in nuxt SSR?

Is there any way of generating a Google : ads.txt file every time i build my SSR project?
There is a module called: sitemap-module from nuxt-community, it is used to generate a sitemap xml file, and that file can be accessed by http://domain.tls/sitemap.xml. and i want something like that.
So currently i'am achieving this by building the project, then manually put ads.txt it in : /var/www/site/.nuxt/dist/client/,
The problem with this is that everytime i rebuild the project i loose /var/www/site/.nuxt/dist/client/ folder then i have to add ads.txt file again.
I would like to know how i can hook up my code to tell nuxt to generate ads.txt file and put it in /var/www/site/.nuxt/dist/client/
Not sure if it makes sense, but i hope someone will understand.
Place your ads.txt file in a directory named static as mentioned here. All the contents of the static directory can be accessed via example.com/{filename.extension}
If you are not using nuxt.js and just using vue js, place it in a public directory right next to src directory.
Haven't found any way of solving this in nuxt, so i decided to redirect all https://domain.tld/ads.txt in Nginx configuration.
/etc/nginx/sites-available/default.conf
#redirect all txt request
location ~* ^.+.(txt)$ {
root /var/www/other.files/;
}
So i think i'll stick to it.

Purpose of a virtual path prefix in Express

The way to serve static on the server side seems pretty straightforward in Express:
To serve static files such as images, CSS files, and JavaScript files,
use the express.static built-in middleware function in Express.
Pass the name of the directory that contains the static assets to the
express.static middleware function to start serving the files
directly. For example, use the following code to serve images, CSS
files, and JavaScript files in a directory named public:
app.use(express.static('public'))
Now, you can load the files that are in the public directory:
http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html
Express looks up the files relative to the static directory, so the
name of the static directory is not part of the URL.
To use multiple static assets directories, call the express.static
middleware function multiple times:
app.use(express.static('public'))
app.use(express.static('files'))
Express looks up the files in the order in which you set the static directories with the express.static middleware function.
I get the idea of a virtual path prefix, but why would you use it?
To create a virtual path prefix (where the path does not actually
exist in the file system) for files that are served by the
express.static function, specify a mount path for the static
directory, as shown below:
app.use('/static', express.static('public'))
Now, you can load the files that are in the public directory from the /static path prefix.
http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html
I know it's a little late, but hope this could help you. BTW, I take no credit for the answer, just went to dive a little deeper on your concern and found this.
This technique comes in handy when providing multiple directories to serve static files. The prefixes are used to help distinguish between the multiple directories.
If you would like to dig deeper. I found it on this link:
https://guide.freecodecamp.org/nodejs/express/

How to change path in the laravel config if It's located in the folder upper then public?

I have laravel 5 and config file in the config folder and My css located in the resources folder which is a same level with public folder where located index.php. Virtual Host Apache config looks to the public folder as a root site directory, but in this situation I cannot declare correct path from /public/index.php to the resources folder.
From one side I can try easy way and just relocate public folder into root of the laravel, but I don't like this way, any ideas?
Use resource_path('path/to/your/css')
https://laravel.com/docs/5.3/helpers#method-resource-path
EDIT
The most logical is to include your stylesheets in your public folder though. If you need to style a page, the style is public anyway. So why not put in the public folder. There's 2 options to do this:
Do it manually by just copying/moving the files
Use an automated tool like Gulp or Laravel's own Elixir, which provides a really easy way to copy your assets.
add this code in public/index.php
$app->bind('path.public', function() {
return __DIR__;
});