Tomáš Zato
Tomáš Zato

Reputation: 53267

How to alias some path in Express?

I have a directory with HTML files which I serve with express:

var express = require('express');
var app = express();
var server = require('http').Server(app);

server.listen(8080);
// Serve /web subdirectory of this directory
app.use(express.static(__dirname+"/web"));

This allows anyone to access a file in web/ directory to access it via http://X.X.X.X:8080/file.html.

But I also have another directory somewhere else. Basically I would like to serve ../../some_directory as http://X.X.X.X:8080/some_directory/ including any of it's subdirectories.

How can I do it? Is there something like app.use_as("file path", "URL path") to serve path as URL?

Upvotes: 1

Views: 2714

Answers (1)

Freyja
Freyja

Reputation: 40904

You can pass the path to use as the first argument of app.use (reference):

app.use("/some_directory/", express.static(__dirname + "/../../some_directory/"));

This will serve files ../../some_directory/file directory under http://host/some_directory/file, including subdirectories.

Upvotes: 2

Related Questions