Specifying root folder express-http-proxy - node.js

I am using express-http-proxy
basic setup
const express = require('express');
const proxy = require('express-http-proxy');
app.use('/api/', proxy(`http://api-url/`));
app.listen(3000);
works as expected, except static files. they are point to proxy host instead of proxy route
http://localhost:3000/bundle.js
it should be
http://localhost:3000/api/bundle.js

I am assuming that bundle.js is a file hosted on the proxy host. Wherever it is referenced you may have to use relative paths so that it does not go to the root of the url.

Related

Express server and NGINX proxy only serving index.html, no other files

I have a very simple express app which serves everything in the build folder for my react app. Here's the entire thing:
const express = require("express");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
app.use(express.static(process.env.PUBLIC_DIR));
app.use(express.json());
app.listen(port, () => {
console.log(`Server is running on port ${port}...`);
});
When running this on my local machine, it works fine. No issues.
On my EC2 instance, I'm using NGINX as a reverse proxy. Here's what the config in my default sites-available file looks like:
location / {
proxy_pass http://localhost:5000/;
}
location /upvotes {
proxy_pass http://localhost:5002/;
}
When you just go to the main site, another express app on 5000 serves a totally unrelated Gatsby project. That works fine, no issues.
When you go to /upvotes, this express app on 5002 does serve the index.html file perfectly fine, but it doesn't serve any of the accompanying .js and .css files that are also in that directory.
Does anyone know why this could be happening?
I eventually gave up and combined the two express apps into one and handled the /upvotes route using express. 🤷

How can I serve a static file using Koa?

I want to implement universal links in my project and I need to serve a json via Koa, which is a static file named apple-app-site-association.
My file is located in /assets/apple-app-site-association/apple-app-site-association folder.
My concern is that I cannot access this https://myprojectlink/apple-app-site-association.
What I have at this moment:
const path = require("path");
const Koa = require("koa");
const mount = require("koa-mount");
const serve = require("koa-better-serve");
app.use(mount("/apple-app-site-association", serve(path.resolve(__dirname,"../../../assets/apple-app-site-association/apple-app-site-association"))));
I get Not Found, it seems like I cannot serve it in the right way.
What can I do?
Thank you very much in advance.
The koa-static module is what you are looking for. You can use this to serve a single file or entire directory to a given path. Here are a couple of examples that should help:
Serving Files With koa-static
To serve files just pass the koa-static middleware to the koa's middleware stack with app.use().
Serve an Entire Directory
Here we are serving the entire /static directory
const Koa = require('koa')
const serve = require('koa-static')
const path = require('path')
const app = new Koa()
app.use(serve(path.join(__dirname, '/static')))
app.listen(3000)
Serve a Single File
Here we are serving a single file, for example, the data.json file inside of the /static directory
const Koa = require('koa')
const serve = require('koa-static')
const path = require('path')
const app = new Koa()
app.use(serve(path.join(__dirname, '/static/data.json')))
app.listen(3000)
Serve Directory or File on a Given Path
Use koa-mount to mount koa-static to a given path. For example, here we mount the entire /static directory to be served on the /public path
const Koa = require('koa')
const serve = require('koa-static')
const mount = require('koa-mount')
const path = require('path')
const app = new Koa()
app.use(mount('/public ',serve(path.join(__dirname, '/static'))))
app.listen(3000)
serve (koa-better-serve), like most static server middlewares for Node frameworks, takes a path to a directory, not a path to a single file. You can also get rid of the mount() call, koa-mount is for mounting other Koa apps as middleware.
app.use(serve(path.resolve(__dirname,"../../../assets/apple-app-site-association/")));
The official method for static-file serving is https://www.npmjs.com/package/koa-static, you can see the documentation there.

node express cannot GET static content

Im running a node app inside of /opt/myapp directory.
I have haproxy in front content switching on path_beg /myapp
backend server is listening on port 3000
directory structure:
/opt/myapp
index.js
package, modules
static
public
myfile.html
const express = require("express");
const path = require('path');
const app = express();
app.listen(3000, () => console.log("listening on 3000 "+__dirname+" "+process.cwd()));
app.use(express.static(__dirname+'/static/public')); //nope
//app.use(express.static('..'+'/static/public')); //nope
//app.use(express.static(path.join(__dirname, '/static/public/'))); //nope
Where __dirname outputs /opt/myapp and process.cwd() outputs /opt/myapp
I tried both concantenation and path.join with same results. Cannot GET myfile.html
curl directly on the server to http://host.com:3000 does work by returning the page,
but from browser (in front of haproxy), http://host.com/myapp/myfile.html does not work.
I suppose that I can remove the /myapp from the path in haproxy on the backend, but is there a way with express that i can account for the base directory?
This worked:
app.use('/myapp/',express.static('static/public'));

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?
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.

Can't serve up a static directory using Node 0.9 and latest express 4.11.2

Using the following simple Node server
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public'))); // "public" off of current is root
app.listen(3000);
console.log('Listening on port 3000');
I have of course put a 'public' directory in my root and also dropped a index.html file in it but when i point my browser to
http://localhost:3000/public/
I get
Cannot GET /public/
The problem is how you're mounting express.static middleware. In your example you're mounting it to the root of your application.
So, in your example you should be able to access content of /public directory from http://localhost:3000/.
So, all you need is to specify a mounting point for express.static middleware:
app.use('public', express.static(path.join(__dirname, 'public')));
You should also keep in mind that express.static don't have a default index page. So, if you have no index.html in your public directory you'll get 404 anyway (express.static won't list your files like apache do).
This one caught me out as well when starting with express.
http://localhost:3000/public/ won't be accessible.
For example lets say you have a css folder inside of the public directory with a file called styles.css
Your browser would be able to access that file at http://localhost:3000/css/styles.css.
The express documentation on express.static can be found here: app.use (under the two example tables)
edit 1: See this answer here.
edit 2: #Leonid Beschastny answer is correct, I didn't see that a path to a folder was missing in app.use(express.static(path.join(__dirname, 'public')));

Resources