我有一个需要为特定路线呈现动态页面的要求。 这些路由将首先添加到正在运行的服务器中,以便可以在同构react应用中生成相应的路由页面并动态提供服务。
对于当前设置,一切正常,但是我坚持在服务器运行时如何动态地重新加载路由。
尝试以下代码,并且可以运行,但仅在服务器启动之前。
var articlesEndpoints = ['/article2', '/article3'];
articlesEndpoints.forEach(function (name) {
console.log(name);
app.get(name, function (req, res) {
res.send(name);
});
});
因此,首先,我需要一种机制来保存某个位置的动态路由(/article1、article2、.............N条)并触发服务器重启。
在服务器重新启动之前,应将这些URL加载到Array中,并在重新启动之前添加动态路由。
尽管尝试使用express-route-refresh
节点模块,但无法正常工作。
我需要建议应该在哪里更新动态路由列表(例如File:文件)或任何其他替代方法,以便服务器每次重新启动前都读取更新的文件。
如果我们将路由保存为平面列表,则任何适当的机制都可以在更新文件时自动实现。
任何帮助都会对我有帮助。
下面是我的server.js
/*globals process, console */
/*eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
if (process.env.NODE_ENV === "production" || process.env.NODE_ENV === "preprod") {
require('newrelic');
}
import express from 'express';
import dotenv from 'dotenv';
import userAgent from 'express-useragent';
import compression from 'compression';
import cookieParser from 'cookie-parser';
import routes from './src/routes/';
import middleware from './src/utils/middleware';
import redirection from './src/utils/redirection';
import bodyParser from 'body-parser';
import path from 'path';
import cluster from 'cluster';
import os from 'os';
import { ASSET_BASE } from './src/config/common_conf';
if (cluster.isMaster) {
let cpuCore; // Total no of CPU Cores.
// Master cluster will setup the forks based on no of cores available.
if (process.env.NODE_ENV === "development") {
cpuCore = 1; // Total no of CPU Cores.
} else {
cpuCore = os.cpus().length; // Total no of CPU Cores.
}
console.log("Master cluster setting up " + cpuCore + " workers.");
for (let i = 0; i < cpuCore; i += 1) {
// Fork the process.
cluster.fork();
}
cluster.on("online", (worker) => {
console.log("Worker " + worker.process.pid + " is online");
});
// When process dies, replace it with a new fork.
cluster.on("exit", (worker, code, signal) => {
console.log("Worker " + worker.process.pid + " died with code: " + code + ", and signal: " + signal);
console.log("Starting a new worker");
cluster.fork();
});
} else {
// Config required for Env variables.
dotenv.config();
// Create the Express app.
const app = express();
app.use(function (req, res, next) {
"use strict";
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "content-type");
next();
});
// Sync the redirection URLs
redirection.syncCache();
//To parse cookies
app.use(cookieParser());
// Middleware for parsing incoming request bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Compression middleware which attempts to compress response bodies
app.use(compression());
/* Do not disturb the order of Middleware */
// Middleware: To identify the browser information
app.use(userAgent.express());
// Middleware: Utility middleware
app.use(middleware);
// Middleware: Redirection middleware
app.use(redirection.isRedirectable);
// Make all the files in build_public folder availale to the outside world.
app.use(`/${ASSET_BASE}`, express.static(path.resolve(__dirname, 'build_public')));
app.use(`/${ASSET_BASE}`, express.static(path.resolve(__dirname, 'images')));
/* var articlesEndpoints = ['/article2', '/article3'];
articlesEndpoints.forEach(function (name) {
console.log(name);
app.get(name, function (req, res) {
res.send(name);
});
}); */
// Routes to be served
routes(app);
app.listen(process.env.PORT, () => {
console.log('Started SEO server on ', new Date().toISOString());
console.log('Env:', '\x1b[31m', process.env.NODE_ENV, '\x1b[0m');
console.log('Port:', '\x1b[31m', process.env.PORT, '\x1b[0m');
});
}
当前路线文件如下:
module.exports = function (app) {
app.get('/', (req, res) => {
res.status(200).json({message: "ok"});
});
app.get('/abc-directory/:pageId?', [commonMw], abcRenderer.renderRoutesABC);
}