lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.2k stars 85 forks source link

Virtual folder / Rewriting to different folder? #85

Closed IceReaper closed 6 years ago

IceReaper commented 6 years ago

I think, i do not need to write much: ws --rewrite '/assets/* -> ../Core/assets/$1' --directory www

I need this to avoid copying my assets into the www development environment. However, it seems this gets always blocked by the resolve-path module. Any way to implement this behavior without copying the assets?

More explanation: I have a multi platform project. /Core contains the assets and main typescript code. /Web contains the browser variant, /Desktop an nwjs build, /Mobile for PhoneGap etc. There is a build process copying files correctly, however while developing, im using the local-web-server as simple "run from source"-solution, so you dont need to rebuild the whole project every time. However this requires me to redirect the assets to their source path, instead of copying all the content over and over.

75lb commented 6 years ago

If you specify that www is the base --directory, then only files within the www folder can be served (this is for security reasons, else a browser user could request any file he liked from a servers file system).

Rewrites to any folder higher up than www will fail, you can only rewrite to a folder within www.

IceReaper commented 6 years ago

I understand this in a public environment, but im just using this for a local testing environment. Well, thanks for the response, was hoping to find an npm module which allowed me to do that, seems noone allows this. Due to your answer i now hacked it together like this, which is enough for my use case:

const http = require('http');
const fs = require('fs');
const mime = require('mime');

http.createServer(function (request, response) {
    var filePath = request.url;

    if (filePath === '/') {
        filePath = 'www/index.html';
    } else if (filePath.substr(0, 8) === '/assets/') {
        filePath = '../Core' + filePath;
    } else {
        filePath = 'www' + filePath;
    }

    if (!fs.existsSync(filePath)) {
        response.writeHead(404);
        response.end();
    } else {
        fs.readFile(filePath, function (error, content) {
            response.writeHead(200, {'Content-Type': mime.getType(filePath)});
            response.end(content, 'utf-8');
        });
    }
}).listen(8080);
75lb commented 6 years ago

yes, that will do it.

Another solution is to create symlinks under the www directory to folders located elsewhere in the filesystem, then create redirects to resources under the symlink. E.g.

$ cd www
$ ln -s ../core/something

You now have a www/something path you can re-route to.

The only other way i can think to solve it is to run ws twice, once in each location. So run one server like this (folder names may vary):

$ ws --directory www --port 8000 --redirect '/assets/* -> http://localhost:8010/'

Another server like this.

$ ws --directory core --port 8010

I've used both these techniques in the past.