简体   繁体   English

如何在子文件夹/子路径中运行 Express 应用程序

[英]How to run an Express application in a subfolder/subpath

So, I have a very simple Express.js application.所以,我有一个非常简单的 Express.js 应用程序。 My entry point, server.js , is reproduced below.我的入口点server.js复制如下。

const express = require('express');
const app = express();


app.get('/', (req, res) => {
    res.status(200).send({ "message": "Welcome to the application. Express JS is outputting this JSON. " });
});

app.get('/all', (req, res) => {
    res.status(200).send({ "message": "All the things. " });
});


app.listen(3000, () => {
    console.log("Server is listening on port 3000");
});

When I run this program with the command node server.js it works fine, and I can access the app at http://localhost:3000 .当我使用命令node server.js运行该程序时,它工作正常,我可以在http://localhost:3000访问该应用程序。 Similarly, when I deploy it to a LAN server, I can access its IP and reach the app like http://192.168.100.105:3000 .同样,当我将它部署到 LAN 服务器时,我可以访问它的 IP 并访问像http://192.168.100.105:3000这样的应用程序。

Now, what if instead, I want the application to run at http://192.168.100.105/app1 ?现在,如果我希望应用程序在http://192.168.100.105/app1运行,该怎么办? One way to do it would be to app the Express app to run on port 80 and reconfigure all routes to use app1 as the new base URL, but I do not have the privilege to (or otherwise cannot) override the application running on the default port.一种方法是将 Express 应用程序应用到在端口80上运行并重新配置所有路由以使用app1作为新的基础 URL,但我没有特权(或不能)覆盖在默认值上运行的应用程序港口。 So, is there any other way to achieve the same thing without running it on port 80 ?那么,有没有其他方法可以在不在端口80上运行的情况下实现相同的目标?

There are two approaches:有两种方法:

  • Have the software you want to access on port 80 actual listen on port 80 (you have ruled this out)让你想访问的软件在 80 端口上实际监听 80 端口(你已经排除了这个)
  • Have some other software listen on port 80 and relay requests to your software其他一些软件在端口 80 上侦听并将请求中继到您的软件

The typical approach with the latter is to use an HTTP server (such as nginx) configured to act as a reverse proxy.后者的典型方法是使用配置为充当反向代理的 HTTP 服务器(例如 nginx)。 It can be configured to map a specific path rather than everything.它可以配置为 map 一个特定的路径而不是一切。

You could also use something like port forwarding with iptables (which effectively makes the software listen on port 80 as well as its real port).您还可以使用类似 iptables 的端口转发(这有效地使软件侦听端口 80 及其真实端口)。


Any of these options will require sufficient privileges so, given the limitations you hint at, this is probably impossible for you.这些选项中的任何一个都需要足够的权限,因此,鉴于您暗示的限制,这对您来说可能是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM