How to handle relative paths in node.js / express?

I wrote a website in node.js and express. Now I have configured lighttpd to use the node.js server with a subdirectory:

$HTTP["url"] =~ "^/app/" { proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => 3000 ) ) ) } 

When I open http://localhost/app/ , I get a 404 error because I wrote something like this:

 app.get('/', function (req, res){ res.render('index'); }); 

Is there a better way to change strings like:

 var relPath = '/app'; app.get(relPath + '/', function (req, res){ res.render('index'); }); 

?

+6
source share
1 answer

As Ryan said, the solution is:

 app.use('/app', app.router); 

If you use, for example, express.static or express.favicon, you must specify app.use also the path:

 app.use('/app', express.favicon(__dirname + '/public/images/favicon.ico')); app.use('/app', express.static(__dirname + '/public')); 

Remember to write '/ app' before each internal link installed in your html.

+4
source

Source: https://habr.com/ru/post/919950/


All Articles