Serving express files from base directory - express

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).

Related

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

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.

Apache server cannot find local file [duplicate]

I'm working on a Flask extension from which I want to create a directory in the project's root path on the file system.
Suppose we have this directory structure
/project
/app
/tests
/my_folder
manage.py
my_folder should be created dynamically by the extension, which is a test utility and wraps the application under test in the /tests directory. However, I'm struggling to determine the project's root path within my extension.
For now, I am trying to guess the path from the run file:
def root_path(self):
# Infer the root path from the run file in the project root (e.g. manage.py)
fn = getattr(sys.modules['__main__'], '__file__')
root_path = os.path.abspath(os.path.dirname(fn))
return root_path
This obviously breaks as soon as the tests are run from within the IDE instead of the manage.py. I could simply infer the project's root relative to the app or tests directory, but I don't want to make any assumptions regarding the name or structure of these directories (since multiple apps might be hosted as subpackages in a single package).
I was wondering if there is a best practice for this type of problem or an undocumented method which the Flask object provides (such as get_root_path).
app.root_path contains the root path for the application. This is determined based on the name passed to Flask. Typically, you should use the instance path (app.instance_path) not the root path, as the instance path will not be within the package code.
filename = os.path.join(app.instance_path, 'my_folder', 'my_file.txt')
app.root_path is the absolute path to the root directory containing your app code.
app.instance_path is the absolute path to the instance folder. os.path.dirname(app.instance_path) is the directory above the instance folder. During development, this is next to or the same as the root path, depending on your project layout.

Relative path inside a module

I have this module which needs a specific file to work. You can pass the path of the file you want to use or not. If you don't, then a default file is taken. That default file is located at the resources folder, so I typed the path as: "resources/data/type-graph.txt". The problem is that does not work because it takes my CWD as root directory.
Do you know how to make the path relative to the module dir?
Any suggestion is appreciated :).
You should take a look at the Modules documentation page.
There this example is given to access a file placed in the resources folder:
my $template-text = %?RESOURCES<templates/default-template.mustache>.slurp;
You also need to list the file in META6.json so the file will be accessible once the module is installed.
{
...,
"resources": [ "templates/default-template.mustache"]
}
As guifa noted in a comment %?RESOURCES works with individual files, not directory structures. It makes no guarantees of how the files are actually stored. So %?RESOURCES<templates>.dir will not work.

In VueJS is there a way to read environmental variables or config values into a static/ file during build step

Background:
Using VueJS, specifically in regards to PWA template https://github.com/vuejs-templates/pwa
There is a build step npm run build which bundles the project and transpiles any Vue into a distribution browser JS.
The files in /static/ are "static" and just copied into dist, but I am wondering if it's possible to template it at all, or read in some dynamic values.
Question:
Is it possible to have static files that servce under /static in the url, but also during build can accept dynamic values?
More context:
The problem is Vue compiles everything into the dist directory.
All non-static assets are cached and get a unique url each build, whereas static files (I know this is configurable, but you arguably want your non-static assets to have caching) have absolute paths.
Server Routing to map a file in /static/ to a cached dynamic file is outside of Vue. The question pertains to needing to host some "absolute pathed files" (static), but some files might have internally 1-2 urls that need to change in the files depending on what config is used, dev, prod, staging.. just as an example of the use case.
The solution I found was to use CopyWebpackPlugin which comes natively inside build/webpack.prod.conf.js
This is the plugin that copies files from static into dist/static.
You can use the process.env.NODE_ENV to allow you to copy specific files from static into dist.
I decided just to keep environment specific copies of the files with values changed, but you could easily add code to that file to parse and copy over whatever specific files you want.
I think most people put dynamic configuration values in a file under public/ then use javascript fetch to load those values in Vue components. Webpack will copy the files in public/ to the web root (dist/) and it will avoid compiling those config values into the minified javascript. If you put files in static/ and use import or require to load them into Vue components then webpack will resolve those during build time and compiling them into the minified Javascript - which is probably not what you want.

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/