I have 3 servers running as a cluster managed by flynn (a bit like heroku) which I can attach a shared drive to so that in theory they all have access to the same files.
However I'm a bit unsure if it's possible to access a mounted drive from my nodejs app to save and access files.
Can anyone shed some light on if this is possible and roughly how I'd go about doing it.
With node.js, your file system path has literally nothing to do with the URLs that your server supports. node.js servers do not serve ANY files by default (unlike some other servers).
If you want an incoming browser request for http://example.com/uploads/test.jpg to read a file from /mnt/shared/uploads, then you have to create a web server route handler that includes the incoming path http://example.com/uploads/test.jpg and then reads the data from /mnt/shared/uploads and writes that data out as the http response.
Depending upon what web server environment you are using, there are helpers to do that mapping. For example, express has express.static() that helps with some auto mapping. But, the web server by itself does not server any files like this automatically.
So, if what you want is that all incoming requests for http://example.com/uploads/* will be read from /mnt/shared/uploads/*, then you can use express.static() to help you do that like this:
app.use("/uploads", express.static("/mnt/shared/uploads"));
This will take any path it finds after /uploads and look for that path in /mnt/shared/uploads. If found, it will automatically serve the content as static content.
So, it would work like this with the incoming URL shown first and the place express.static() would look for a matching file:
/uploads/test.jpg ==> /mnt/shared/uploads/test.jpg
/uploads/bob/test.txt ==> /mnt/shared/uploads/bob/test.txt
Related
I'm working on a project that involves both front-end (react.js) and back-end (node.js). The back-end had been uploaded to AWS.
I'm trying to fetch an image stored in the back-end.
Assuming the path to the image in AWS is : /home/ubuntu/path/to/image.jpg
and the url for the back-end is: 123.123.123.123:4000
How can I fetch the image?
Running a GET request in Postman with the following url -
http://123.123.123.123:4000/home/ubuntu/path/to/image.jpg
resolves in a Cannot GET error.
Usually any files you wish to serve need to be in the web root of the server. You don't say, at all, how your AWS Ubuntu server is configured so it is really hard to say what the path should be.
In general though the web root would be something like /var/www/public/ - so using that as an example you would need to move your images into this path - e.g. /var/www/public/path/to/image.jpg
Then the URL to the image would be 123.123.123.123:4000/path/to/image.jpg
In general, without an Alias or symlink with FollowSymLinks or some such, any file you wish to serve needs to be in the web root of the server and resources outside of this path are not web accessible - for obvious security reasons.
You could try and Alias the path to your images into the web root i.e. Alias /home/ubuntu/path/to/ /var/www/public/images/ which should make the path to the image 123.123.123.123:4000/images/image.jpg however, again, this would depend on your server set-up.
This may seem like an odd/broad question, but how does a server know not to render Express.js files and not to expose the content similar to how anyone can see a javascript file, and read the script being executed. Do node servers like Heroku protect them ? Sorry just new to express and node. Is it similar to how PHP syntax/scripts are hidden and protected in a Apache server?
It depends on the server configuration. On a poorly configured server, the .js files might be accessible.
With a nodejs/expressjs server you define a base folder that contains public files, e.g. public and files outside of that public folder are not visible, because the server doesn't serve them to the outside. If you configure the wrong directory, e.g. ., then the expressjs code files would be available to browsers and would be rendered as-is to them, potentially revealing unsafe data like configuration, passwords and so on. Since the default configuration and all code examples make sure that public is defined as the public folder, the risk of accidental misconfiguration is low.
If you run an apache httpd or other webserver on the same host, you have to make sure that the node application is not inside the webroot of any vhost, otherwise the files might also be visible, because to the apache httpd they also look like simple static files, ready to be sent as-is to the browser.
It is different from PHP files, at least in the case of apache httpd or nginx, because those are usually configured so that PHP files are files to be executed, not static files to be served to the outside. However, if the apache httpd or nginx doesn't know about PHP, either because it isn't installed or isn't configured, then PHP files inside the webroot would also be shown to the public as-is. Display of files for the apache httpd can be prevented using .htaccess files.
I'm a beginner in Node.js and I have implemented an express app with both client module and server module as a part of single project. I start server by calling
node server.js
I'm using express.static to refer to the client code and getting index.html of client.
app.use(express.static(__dirname + "/client"));
Now, what if I don't want client and server to be a part of same project? How should the express.static statement be written? Client's project can be located in some other directory and "__dirname" would not work in that case. How should client and server be made independent of each other's directory location?
In production it is standard to expose your client app with an http server which is better in serving static files, e.g. nginx.
All you're doing is serving files with express as though its a standard HTTP server. Its popular to serve these files with something like Nginx instead of relying on express to serve the files. Nginx scales better for this kind of thing. Its also possible to use a CDN to distribute your content to get it closer to your end user.
Either way, using express isn't horrible, but if you plan to scale its probably easier to scale the backend independent from the frontend because the backend is going to be a lot more resource hungry than a process serving static files.
If you are talking about relocating static files on your hdd but still serving them from express then you can use path module.
How should the express.static statement be written? Client's project can be located in some other directory and "__dirname" would not work in that case.
var path = require('path');
var pathToResources = path.join('/path/to/resources', 'client');
app.use(express.static(pathToResources));
But if you want to use different server (origin) to serve content then you don't need to specify this (just make sure to set up correctly cross-origin on the other server. In that case one server would serve as static content server and another as ```express`` backend api server.
I have one linux server for my website which contain php code,database and files.
files are being uploaded and downloaded by enduser for their individual tasks. My website is working fine but as website evolove these files volume will be increased so my webserver will be overloaded.
So I want to use seperate server for files so that burden on 1 webserver will be decrease and files will be downloaded and uploaded on another server.
Can anyone suggest me best way to achieve that. I know the files can be transferred to another server by FTP functions of php just after uploading through website but doesn't seems a correct way.
option 1 (simple): you have the upload form post to the second server rather than handling the upload with the server that runs the rest of the application
option 2: (in most cases wrong approach) have the receiving script on server 1 store it in the db, and server 2 when checking for the download pull it from db and cache it locally.
option 4: (my favorite) put a reverse proxy (for example varnish) on server 1, let the application run on server 2, and have the proxy cache static files for , so the reverse proxy will handle the downloads (and other static files like images, javascript etc.) if available. This allows you for a few other tricks to improve your performance as well (like caching sites that can be cached). https://www.varnish-cache.org/
I'd like to create a simple site on NodeJS. For example, it has two files (app.js - main application file) and router,js (a url file). I'd like to know - if it possibke for anyone just to access mydomain.com/router.js to get the source code of my application? I'm asking 'cause for example in PHP you cant just access to php, as you know server just gives you the result of working of this PHP-file, but not the file itself. So, how to make my nodejs-app invisible for public access? Thanks!
I make sure that all files for Node.js are never in a path that is served by another web server such as Apache. That way, there is little danger of the source ever being served by accident.
My node program's and files go in /var/nodejs with a sub-folder for each application in Node. By default of course, Node will not serve ANYTHING unless you tell it to.
At the root of my Apache configuration, I make sure that ALL folders are secured so that I explicitly have to enable serving on any folder structure even under the /var/www folders that I use for all Apache sites.
So you are pretty safe with a default setup of Node and Apache as long as you keep the folders separate. Also Node will not serve source code accidentally, you would have to set up a Node server that read the file as text and wrote it to the http stream.
That depends on how you are using Node.js and what you are using for a web server in front of it. Unlike PHP running as CGI or as a module in Apache, node and the node application itself is a server.
If you have a webserver with your node source directory exposed then the url you provided in the question will most likely result in your source code being served. Even if you were using Apache and proxying to node, there is usually no output filter involved. Instead requests are passed to the backend node server which interprets them.