我想根据子域提供不同的静态内容
app1.example.com
app2.example.com
app3.example.com
const app1path = path.join(__dirname, "public", "app1");
this.app.use(vhost("app1.localhost:1234", serveStatic(app1path)));
但是我刚得到Cannot GET /
。我在这里做什么错了?
server.js
import App from "./app";
class Server {
constructor() {
this.app = new App();
this.instance = this.app.instance;
this.config = this.app.config;
this.server = this.instance.listen(this.config.port, "0.0.0.0");
console.log("Server Running On: 0.0.0.0:" + this.config.port);
}
}
export default Server;
app.js
import express from "express";
import Config from "./config";
import Router from "./routes";
import cors from "cors";
import morgan from "morgan";
class App {
constructor() {
// Init
this.instance = express();
this.config = new Config();
// Middleware
this.instance.use(morgan("combined"));
this.instance.use(cors());
this.instance.use(express.json());
this.instance.use(express.urlencoded({ extended: true }));
// Routes
this.router = new Router(this.instance);
}
}
export default App;
routes.js
import Config from "./config";
import CoreRouter from "./app/core.router";
import vhost from "vhost";
import serveStatic from "serve-static";
import path from "path";
class Router {
constructor(app) {
this.app = app;
this.config = new Config();
this.app.use("/api/v1/core", CoreRouter);
const app1 = path.join(__dirname, "public", "app1");
this.app.use(vhost("app1.localhost", serveStatic(app1)));
}
}
export default Router;
/ etc / hosts
127.0.0.1 localhost
127.0.0.1 app1.localhost
更新:我已经提供了更多代码,希望可以帮助我调试此问题。在浏览器中导航到app1.localhost:1234
会返回Cannot GET /