MCPcopy Create free account
hub / github.com/decaporg/decap-cms / runDevServer

Function runDevServer

packages/decap/src/dev/server.ts:44–104  ·  view source on GitHub ↗
(options: DevServerOptions = {})

Source from the content-addressed store, hash-verified

42 * `npx decap-server`. Lets the CMS read and write the repository on disk.
43 */
44export async function runDevServer(options: DevServerOptions = {}): Promise<Server> {
45 dotenv.config();
46
47 // The middlewares read their settings from the environment, so flags are
48 // applied there rather than threaded through every call.
49 if (options.port !== undefined) process.env.PORT = String(options.port);
50 for (const [option, variable] of Object.entries(ENV_FOR_OPTION)) {
51 const value = options[option as keyof typeof ENV_FOR_OPTION];
52 if (value !== undefined) process.env[variable] = value;
53 }
54
55 const app = express();
56 const port = parseInt(process.env.PORT || '8081', 10);
57 const host = process.env.BIND_HOST;
58 const logger = createLogger({ level: process.env.LOG_LEVEL || 'info' });
59 const middlewareOptions = { logger };
60
61 registerCommonMiddlewares(app, middlewareOptions);
62
63 const mode = process.env.MODE || 'fs';
64 if (mode === 'fs') {
65 await registerLocalFs(app, middlewareOptions);
66 } else if (mode === 'git') {
67 await registerLocalGit(app, middlewareOptions);
68 } else {
69 throw new Error(`Unknown proxy mode '${mode}'. Use fs or git.`);
70 }
71
72 // The "listening" lines are matched by the e2e suite (cypress/plugins/proxy.js)
73 // and possibly by people's own scripts: keep their wording.
74 if (host) {
75 const server = await listen(app, port, host);
76 logger.info(`Decap CMS Proxy Server listening on ${host}:${port}`);
77 if (!isLoopbackAddress(host)) {
78 logger.warn(
79 `The proxy has no authentication: anyone who can reach ${host}:${port} can read and write files in your repository`,
80 );
81 }
82 return server;
83 }
84
85 // The server can write any file in the repository, so by default it only
86 // listens on this machine. The CMS calls http://localhost:8081, and
87 // localhost resolves to either loopback address, so listen on both.
88 const server = await listen(app, port, '127.0.0.1');
89 try {
90 // The bound port, not `port`, which may be 0 for "any free port".
91 const ipv6Server = await listen(app, (server.address() as AddressInfo).port, '::1');
92 server.on('close', () => ipv6Server.close());
93 } catch (e) {
94 // IPv6 disabled on this machine: browsers fall back to 127.0.0.1.
95 if (!['EADDRNOTAVAIL', 'EAFNOSUPPORT'].includes((e as NodeJS.ErrnoException).code!)) {
96 server.close();
97 throw e;
98 }
99 }
100 logger.info(
101 `Decap CMS Proxy Server listening on port ${port} (this machine only; pass --host 0.0.0.0 to expose it)`,

Callers 3

index.jsFile · 0.85
mainFunction · 0.85
server.spec.tsFile · 0.85

Calls 7

createLoggerFunction · 0.85
registerLocalFsFunction · 0.85
registerLocalGitFunction · 0.85
listenFunction · 0.85
isLoopbackAddressFunction · 0.85
closeMethod · 0.80

Tested by

no test coverage detected