What is the Meteor server-side path to /public?
Asked Answered
C

5

6

On the Meteor client-side, I know that files in the project's public directory are referenced at '/'.

How are they referenced on the server-side?

I am trying to get a directory listing with fs.readdir, but I don't know how to construct the path to get to the server side equivalent of the client side '/images/gallery'.

Any advice?

Canady answered 26/6, 2013 at 19:29 Comment(0)
N
0

When I use the fs-module I just use './public' for my public folder, works fine on my local install.

And then I set it to whatever's correct at the production server using environment vars.

Edit (an example):

This method will return all .HTML files from the public folder:

getHtmlFilesInPublicFolder: function() {

    var files = fs.readdirSync('./public/');

    var cleanedUpFiles = _(files).reject( function(fileName) {
        return fileName.indexOf('.html') < 0;
    });

    return cleanedUpFiles;

}
Nertie answered 26/6, 2013 at 21:40 Comment(6)
So you're saying that fs.readdir(./public, function()) would return a list of files in the public directory on the server side?Canady
Sill not working for me. From the client I do: pages = Meteor.call('DirList'); On the server side I have: DirList: function () { var files = fs.readdirSync('./public/'); return files; } But back on the client, after the call, pages is undefined. I must have missed something?Canady
Yes, Meteor.call is async (always) on the client. So you'd do: Meteor.call('DirList', function(error, result) { console.log(result); });Nertie
That was it! Thank you so much for the help. I'm just getting started with Meteor, and I like it, but it does some things differently than I'm used to in a PHP server-side world. Could I ask you to take a look at this question, too? #17264896Canady
Thank you; I'll try your suggestion. Also, do you have a JavaScript form validator that you would recommend for use with Meteor?Canady
This is not working with the latest version of meteor! Use docs.meteor.com/api/assets.html instead!Uncivilized
D
9

The accepted "./public/" answer does not work for me in Meteor 1.1.

However, Meteor supplies the server path via the meteor_bootstrap.serverDir variable, so to get the public folder path I use the following line:

path.join(__meteor_bootstrap__.serverDir, "../web.browser/app");

This works on my local Windows machine and on meteor.com.

Note that this is the "running" version of your public folder, so - at least in development, I haven't checked this part in production - it's actually a merge of your development "public" folder and all of your client-side JS files. If you have a "config" folder in your project, and a "config" folder in your public directory, the "running" path will include the contents of both.

Dope answered 9/7, 2015 at 22:2 Comment(1)
Just to confirm, this works on my development machine and on the production server created using meteor build.Entremets
R
2

there's an upgrade since the 0.6.5 version of meteor, main.js now chdirs into programs/server in your bundle. So the content of the public directory is here : ../client/app/

the detail on github

Rosamondrosamund answered 23/8, 2013 at 14:49 Comment(1)
I tried '../client/app', but that returned (in meteor 0.6.5.1): ["client","myappname.css","myappname.js","template.myappname.js"]. Is this changed again in 0.6.5.1?Shriner
M
1

I got the absolute path for Meteor project directory using below line of code.

var absPath = process.env.PWD;

I have used this with Meteor 1.4.3.2 and it works perfectly.

Mispickel answered 20/5, 2017 at 22:36 Comment(2)
Sadly, process.env.PWD is undefined, at least on Win10 with Meteor 1.8Some
I can confirm it still works with Meteor 2.7 on linux system.Touslesmois
A
0

If you are using nodes file system library on the client then you are going to be working with your local file system structure and you're files will be referenced by the local path to where ever they reside on your local disk.

For example.. if your project is located at /home/bob/meteor_projects/project1 then your files are located at /home/bob/meteor_projects/project1/public

Aswan answered 26/6, 2013 at 21:33 Comment(2)
I have node.fs running in server code. So I need the server path to public.Canady
yes, the server path IS, where ever it is located on the server.. If you do not have access to the server to know exactly where the application is installed you can always use a period at the beginning of your path to specify that you wish to start at the current directory that the application is executing in. As noted below ./public will give you the public directory within the current directory.Aswan
N
0

When I use the fs-module I just use './public' for my public folder, works fine on my local install.

And then I set it to whatever's correct at the production server using environment vars.

Edit (an example):

This method will return all .HTML files from the public folder:

getHtmlFilesInPublicFolder: function() {

    var files = fs.readdirSync('./public/');

    var cleanedUpFiles = _(files).reject( function(fileName) {
        return fileName.indexOf('.html') < 0;
    });

    return cleanedUpFiles;

}
Nertie answered 26/6, 2013 at 21:40 Comment(6)
So you're saying that fs.readdir(./public, function()) would return a list of files in the public directory on the server side?Canady
Sill not working for me. From the client I do: pages = Meteor.call('DirList'); On the server side I have: DirList: function () { var files = fs.readdirSync('./public/'); return files; } But back on the client, after the call, pages is undefined. I must have missed something?Canady
Yes, Meteor.call is async (always) on the client. So you'd do: Meteor.call('DirList', function(error, result) { console.log(result); });Nertie
That was it! Thank you so much for the help. I'm just getting started with Meteor, and I like it, but it does some things differently than I'm used to in a PHP server-side world. Could I ask you to take a look at this question, too? #17264896Canady
Thank you; I'll try your suggestion. Also, do you have a JavaScript form validator that you would recommend for use with Meteor?Canady
This is not working with the latest version of meteor! Use docs.meteor.com/api/assets.html instead!Uncivilized

© 2022 - 2024 — McMap. All rights reserved.