restify: why serving working directory is not possible? - node.js

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"
}));

Related

express.static does not deliver

I searched through all topics, but could not find a similar issue.
I have a server.js in the folder /build
// /build/server.js
import express from 'express';
const app = express();
app.use('/static', express.static(path.join(__dirname, 'build', 'static'), { maxAge: '30d' }));
and a css file in /build/static/css/my.css.
I start nodemon with build/server.js from the root folder /
I assume that I should get my file via localhost:8080/static/css/my.css
But it returns a 404 and "Cannot GET /static/css/my.css"
What could be the issue?
I tried a lot of different paths, but never got a successful response.
Thanks in advance.
I finally decided to go with __dirname to prevent errors caused by relative paths to the directory the server is started from.
The main problem was that __dirname always returned /.
I added the following lines to my webpack config:
target: 'node', // was already there
node: { // has been added
__filename: false,
__dirname: false
},
You can find a closer description of this behaviour of __dirname in webpack here: https://github.com/webpack/webpack/issues/2010#issuecomment-181256611

RESTIFY not serving static files from subdirs

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'
}));

Subdirectories not being served with express.static in heroku

I'm seeing some really odd behavior where some of my files are correctly being returned by my express/node server (using express.static()), but not files within subdirectories. The frustrating thing is that it works fine using node or foreman locally, it just won't work on heroku. This gist has the main files at play here, and my app structure looks like this:
-app
- index.html
- img/
- base.png
- sub/
- sub.png
- scripts
- base.js
- sub/
- sub.js
- css
- base.css
- sub/
- sub.css
- server
- app.js
The index.html, and base.* files all load fine, it's just the sub.* files that 404. Seems bizarre that express.static would go 1 level deep, but not 2
I've tried a slew of different configurations, including this stackoverflow answer. I have to be missing something simple. Thanks for the help.
UPDATE:
When I console.log the following on server startup on heroku, I get:
path.join(__dirname, '../app') = /app
path.join(__dirname, '/../app') = /app/app
path.normalize(path.join(__dirname, '../app')) = /app/app
path.join(process.cwd(), '../app') = /app/app
Make sure that the sub directories of your directory are added to your Git repository.
You can use heroku run 'ls ~' to help debug the issue (by observing the files on the dyno).
Putting the absolute path did not fix it for me. Your .gitignore may be excluding it.
Try changing your static dir to :
app.use(express.static(path.join(__dirname, '/../app'), { maxAge: 86400000 }));
or
app.use(express.static(path.normalize(path.join(__dirname, '../app')), { maxAge: 86400000 }));
add {{__dirname}}
<link href="{{__dirname}}/stylesheets/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
in your layout.hbs or layout.jde
I've just had the same issue, and I've read all the different answers, some of which may have been important, but, in the end, this is the change I made after which the static content started getting served.
CALENDARSPATH = path.join(process.env.PWD, 'calendars');
...
-app.use(express.static(CALENDARSPATH, { maxAge: 86400000 }));
+app.use('/calendars', express.static(CALENDARSPATH, { maxAge: 86400000 }));

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