RESTIFY not serving static files from subdirs - node.js

I´m trying to serve static files with restify using following code.
app.get(/.*/, restify.serveStatic({
directory: __dirname + '/public',
default: 'index.html'
}));
Calling GET / or GET /index.html works as expected.
Calling any file in a subdirectory of public does not work.
For example calling GET /css/style.css leads to a 404.
The file exists but is not being served.
Any ideas?

Try like this, works fine for me:
app.get(/.*/, restify.serveStatic({
directory: 'public',
default: 'index.html'
}));

Related

express static: Conflict when folder-name equals html-page-name

I serve static content via
app.use(express.static(path.join(__dirname, '../Frontend'),{index:"index.html",extensions:['html']}));
In the folder /Frontend is a file with filename: projects.html and a folder with the foldername: projects. This constellation is conflicting and not working.
How can I serve projects.html on www.example.com/projects and the html-files inside the folder on www.example.com/projects/xyz?
(At the moment www.example.com/projects/xyz works and www.example.com/projects returns a 404. Furthermore there is a 301 redirect from /projects to /projects/)
I ended up with a solution (Thank you all for the comments you posted)
Before:
app.use(express.static(path.join(__dirname, '../Frontend'),{index:"index.html",extensions:['html']}));
After (The order is relevant):
app.use('/projects', express.static(path.join(__dirname,'../Frontend/projects.html')));
app.use(express.static(path.join(__dirname, '../Frontend'),{index:"index.html",extensions:['html']}));
Picking up on my comment, I would suggest a variation of what you used:
app.get('/projects', (req, res) => {
res.sendFile(path.join(__dirname,'../Frontend/projects.html'));
});
app.use(express.static(path.join(__dirname, '../Frontend'),{index:"index.html",extensions:['html']}));
This will be more efficient because the first route will only match /projects, not everything else that starts with /projects.

Serving express static files issue

I am having issues w/ serving static files in my current Express app, although I've done a similar setup in a bunch of other apps.. My folder structure is as follows:
/rootfolder/
/app
package.json
/client
/dist
/static
index.html
/server
/src
index.js
Relevant part of my server/src/index.js:
app.use(express.static(path.join(__dirname, "client", "dist")));
Where __dirname = /rootfolder/app/server/src
And when the user hits the / endpoint:
app.get("/", (req, res) => {
res.sendFile(appRoot.path + "/client/dist/index.html");
});
Where appRoot.path = /rootfolder/app
When I hit the / endpoint, I get a status 200 with the following text:
/rootfolder/app/client/dist/index.html
From what I can tell, the files are coded relative to each other correctly.. Does anyone know what I might be doing wrong?
Thanks in advance!
You're using res.send() instead of res.sendFile()
Also I suggest resolving your path via the path module, instead of concatenating a string.
const path = require('path')
app.use(express.static(path.join(__dirname, 'client', 'dist', 'static')))
And for the response of /:
res.sendFile(path.join(__dirname, 'client', 'dist', 'index.html')))
Try
app.use(express.static(path.join(__dirname,'client','dist')));
It basically gets the root directory and combines it with /client+ /dist + /static to give you the full route, without being relative path.
Now Let's call rootdirectory/client/dist X. That is the main directory for static files
If you have other files that are static but not in the same folder, you will have to give relative path from the X directory
Example:
app.get('/',function(req,res){
res.sendFile('/static/data.txt');
}
In the example above you indicate that the static file(data.txt) is located in the X/static directory.
Therefore => rootDirectory/client/dist/static/data.txt
2nd Example:
Let's say you have a folder in dist called images which you want to only store images.
when you are giving routes, you MUST use /images/filename.extention

restify: why serving working directory is not possible?

I have a restify server with node.js I use to make some development and tests and to do so, I use serveStatic.
I wonder why I cannot use the following configuration without getting 403 errors:
server.get(/.*/, restify.serveStatic({
directory: '.',
default: "index.html"
}));
Although if I make a link to my current dir:
ln -s . serverDir
This will work:
server.get(/.*/, restify.serveStatic({
directory: './serverDir',
default: "index.html"
}));
What is the reason for this ? Security ? Bug ? Software or network limitation ?
Is there something I should know or read about serving static files ?
Can you user __dirname instead of '.' to indicate the current directory?
server.get(/.*/, restify.serveStatic({
directory: __dirname,
default: "index.html"
}));

Node.JS express compile less in different folder

I need to compile *.less files in different folder inside public directory. I've git this dir structure:
/public:
/stylesheets:
/less:
style.less
/css:
style.css
/js
/img
...
In my app configure function I've got following:
app.use(require('less-middleware')({
src: '/less',
dest: '/css',
root: path.join(process.cwd(), 'public/stylesheets')
}));
app.use(express["static"](path.join(process.cwd(), 'public')));
But It does't work. Less files never compiles in css folder. Whats wrong?
PS: I could access to the less folder via path: /stylesheets/less/style.less. But why? Why not just /less/style.less? Thanks for any help!

serving static files with restify (node.js)

I have the following code:
app.js
[...]
server.get(/\/docs\/public\/?.*/, restify.serveStatic({
directory: './public'
}));
server.listen(1337, function() {
console.log('%s listening at %s', server.name, server.url);
});
And I have the following file structure
app.js
public/
index.html
So I try browsing:
http://localhost:1337/docs/public/index.html
and I get
{
code: "ResourceNotFound",
message: "/docs/public/index.html"
}
I tried with several variations, but none of them seemed to work.
I'm sure it should be something pretty obvious I'm missing
restify will use the directory option as a prefix for the entire route path. In your case, it will look for ./public/docs/public/index.html.
The directory option is a prefix for your entire path.
Relative paths are not working correctly in later versions of Restify (I tested 2.6.0-3, 2.8.2-3 - and they all produce the NotAuthorized error)
The solution now becomes:
server.get(/\/docs\/public\/?.*/, restify.serveStatic({
directory: __dirname
}));
And then your static files will need to be in ./docs/public.
(__dirname is a global variable that contains the absolute path of the script you are running)
Based on #NdeeJim's answer, to anyone wondering how to serve ALL the static resources:
server.get(/\/?.*/, restify.plugins.serveStatic({
directory: __dirname,
default: 'index.html',
match: /^((?!app.js).)*$/ // we should deny access to the application source
}));

Resources