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
}));
Related
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
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
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'
}));
I am new to AngularJs and wanted to start learning it. I was going to use Restify as my api/backend and was hoping it was possible to serve static files up for the route /.
app layout is something like this..
/nodesprinkler
node_modules/
public/
css/
main.css
bootstrap.css
js/
angular.js
app.js
...
img/
...
index.html
favicon.ico
server.js
routes.js
...
My server.js looks like so:
var restify = require('restify'),
app = module.exports = restify.createServer();
app.listen(8000, function() {
console.log('%s listening at %s', app.name, app.url);
});
/* Client Side Route */
app.get('/', restify.serveStatic({
directory: 'public',
default: 'index.html'
}));
module.exports.app = app;
routes = require('./routes');
How can i get Restify to serve up my static assets so it'll work like a regular express app works? I know restify is based off express, so there must be something simple that i'm missing. It will serve up / as index.html but any of my css and js files I dont have access to.
try express.static()
before app.listen put
app.use(express.static(__dirname+"/public"))
The docs
Try this:
app.get("/css|js|img/", restify.plugins.serveStatic({
directory: "./public"
}));
app.get(
"/.*/",
restify.plugins.serveStatic({
directory: "./public",
file: "index.html"
})
);
I'm creating my futur startup with the same technologies: Restify (that I rewrite) and Angular JS for the single app view.
I've tried of lots of solutions and the best one for me is :
Keep a WS with Restify (or what you want) WITHOUT any static files... I serve it with a dedicated server (python for dev, NGinx for production).
I know this is not the expected answer but give it a try.
python -m http.server on your angular directory is so simple :p
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"
}));