我试图运行两个或更多的nuxt应用程序使用的高速公路和它的vhost功能。我的目标是在一个端口中运行分配给不同域的多个nuxt应用程序。
这是我试过的。
var vhost =需要量(“express”);var vhost =需要量(“vhost”);var app_one =需要量(“./ app _one/server/index.js”);var app_two =需要量(“./app_2/server/index.js”);.listen(3000);
App One
const express = require("express");
const consola = require("consola");
const {
Nuxt,
Builder
} = require("nuxt");
const app = express();
// Import and Set Nuxt.js options
const config = require("../nuxt.config.js");
config.dev = process.env.NODE_ENV !== "production";
async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(config);
const {
host,
port
} = nuxt.options.server;
// Build only in dev mode
await nuxt.ready();
// Give nuxt middleware to express
app.use(nuxt.render);
// Listen the server
app.listen(port, host);
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
});
}
exports.start_app_one = function () {
start();
};应用程序二
const express = require("express");
const consola = require("consola");
const {
Nuxt,
Builder
} = require("nuxt");
const app = express();
// Import and Set Nuxt.js options
const config = require("../nuxt.config.js");
config.dev = process.env.NODE_ENV !== "production";
async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(config);
const {
host,
port
} = nuxt.options.server;
await nuxt.ready();
// Give nuxt middleware to express
app.use(nuxt.render);
// Listen the server
app.listen(port, host);
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
});
}
exports.start_app_two = function () {
start();
};然后我运行node server.js。当我尝试访问appone.com:3000时,它只显示nuxt加载网页,并在此停止。控制台内没有错误或没有消息。我被困在这一点上,有人能帮我吗?
发布于 2019-12-13 06:48:08
所以我让它成功了。我就是这么做的。
中添加了vhost模块来设置高速公路
以下是用于高速公路的server.js内容
const express = require('express')
var vhost = require('vhost')
const app = express()
const port = 3000
const appone = require('./app_one/server/index.js')
const apptwo = require('./app_two/server/index.js')
// app.get('/', (req, res) => res.send('Hello dear world, this is the host app to show vhost capabilities!'))
app
.use(vhost('appone.com', appone))
.use(vhost('apptwo.com', apptwo))
.listen(3000);
// app.listen(port, () => console.log(`Example app listening on port ${port}!`))npx create-nuxt-app <project-name>
这是两个应用程序的index.js文件。
const express = require("express");
const consola = require("consola");
const { Nuxt, Builder } = require("nuxt");
const app = express();
// Import and Set Nuxt.js options
const config = require("../nuxt.config.js");
config.dev = process.env.NODE_ENV !== "production";
async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(config);
const { host, port } = nuxt.options.server;
// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt);
await builder.build();
} else {
await nuxt.ready();
}
// Give nuxt middleware to express
app.use(nuxt.render);
// Listen the server
// port = 3005
// app.listen(port, host)
// consola.ready({
// message: \`Server listening on http://${host}:${port}\`,
// badge: true
// })
}
start();
module.exports = app;现在我转到每个特定于应用程序的文件夹,运行下面的命令来构建文件。
这两个应用程序都可以使用npm run build
我包括了我的子应用程序所需的模块,因为由于某种原因,没有这些模块就无法工作。
{
"name": "webclient_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.0",
"express": "^4.17.1",
"node-fetch": "^2.6.0",
"nuxt": "^2.10.2",
"vhost": "^3.0.2",
"vue-meta": "^2.3.1"
}
}然后,
第一个应用程序
buildDir: "app_one/.nuxt"第二个应用程序。
buildDir: "app_two/.nuxt"node server.js,这两个应用程序都可以从各自的域(即appone.com:3000和apptwo.com:3000 )访问,没有任何问题。
https://stackoverflow.com/questions/59223227
复制相似问题