Skip to content

Instantly share code, notes, and snippets.

@tsibley
Created September 6, 2022 18:57
Show Gist options
  • Select an option

  • Save tsibley/e044784453e68f851a779c62a8fded10 to your computer and use it in GitHub Desktop.

Select an option

Save tsibley/e044784453e68f851a779c62a8fded10 to your computer and use it in GitHub Desktop.
From bf2fb3d9db142a57031ebdd4134c0d819d5ca910 Mon Sep 17 00:00:00 2001
From: Thomas Sibley <tsibley@fredhutch.org>
Date: Thu, 30 Sep 2021 17:00:43 -0700
Subject: [PATCH] wip! ES modules
---
Procfile | 2 +-
README.md | 10 +++++-----
develop.sh | 2 +-
docs/api.md | 2 +-
package.json | 4 ++--
server.js => server.mjs | 9 +++++----
src/{app.js => app.mjs} | 16 +++++++++++++---
7 files changed, 28 insertions(+), 17 deletions(-)
rename server.js => server.mjs (93%)
rename src/{app.js => app.mjs} (91%)
diff --git a/Procfile b/Procfile
index 9fcf8f0..170939d 100644
--- a/Procfile
+++ b/Procfile
@@ -1 +1 @@
-web: node server.js --verbose
+web: node server.mjs --verbose
diff --git a/README.md b/README.md
index 163c30d..88b0012 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ A list of potential issues is being actively maintained at https://github.com/or
This repo comprises:
- 1. A server (`./server.js`) which serves all the content on [nextstrain.org](https://nextstrain.org), handles authentication and responds to API requests.
+ 1. A server (`./server.mjs`) which serves all the content on [nextstrain.org](https://nextstrain.org), handles authentication and responds to API requests.
1. The [splash & documentation pages](#Splash--documentation-pages), which are built using Gatsby & located in the `./static-site` directory.
1. Code to build a customised version of the [Auspice](#Auspice) client, which is located in the `./auspice-client` directory.
@@ -156,7 +156,7 @@ This uses symlinks both globally and within the local `node_modules/` directory
---
## Nextstrain.org server
-`npm run server` runs `./server.js` which serves all the content on [nextstrain.org](https://nextstrain.org) & handles authentication.
+`npm run server` runs `./server.mjs` which serves all the content on [nextstrain.org](https://nextstrain.org) & handles authentication.
This server decides based on the path, whether to serve:
* the bundled auspice JavaScript file (i.e. the auspice client, built above via `./build.sh auspice`) _or_
@@ -172,16 +172,16 @@ It can make the following API requests to a server:
and as long as there is a server and the reponse is appropriate then the auspice client can visualise the data.
-The nextstrain.org server (`server.js`) sets up GET request handlers for those three endpoints using code imported from `./src/index.js`.
+The nextstrain.org server (`server.mjs`) sets up GET request handlers for those three endpoints using code imported from `./src/index.js`.
The code for these handlers, which is exposed by `./src/index.js`, has been written in such a way that it can be imported by:
-1. The nextstrain.org server: `npm run server` (see `server.js`)
+1. The nextstrain.org server: `npm run server` (see `server.mjs`)
2. The auspice server: `cd auspice-client && npx auspice view --handlers ../src/index.js --verbose` (rarely useful in this case, make sure you've run `npm run build -- auspice` first!)
3. The auspice development server: `npx auspice develop --handlers ./src/index.js --verbose --extend ./auspice-client/customisations/config.json` (useful for auspice development, note the client customisations applied here too!)
> Note that 2 and 3 are running the auspice server locally but modifying it via functionality whereby the default request handlers (for `/charon/...` GET requests) can be overwritten by command line arguments.
-In this case, they're overwriting them with the handlers used by the nextstrain.org server (see `server.js`) and thus the auspice server mimics the nextstrain.org server behavior (fetching datasets from S3, etc.).
+In this case, they're overwriting them with the handlers used by the nextstrain.org server (see `server.mjs`) and thus the auspice server mimics the nextstrain.org server behavior (fetching datasets from S3, etc.).
See the [auspice API documentation](https://nextstrain.github.io/auspice/server/api) for more info.
diff --git a/develop.sh b/develop.sh
index de0be48..b8533ea 100755
--- a/develop.sh
+++ b/develop.sh
@@ -2,7 +2,7 @@
trap 'kill 0' EXIT
-NODE_ENV="dev" node server.js &
+NODE_ENV="dev" node server.mjs &
cd static-site && npm run develop
wait
diff --git a/docs/api.md b/docs/api.md
index 9ff8cd7..4f38bb5 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -8,7 +8,7 @@ For a summary of the requests and responses please see [the auspice server docs]
## Serverside Handlers
-The handlers are attached within `server.js` and are typical [express](https://expressjs.com/en/api.html) callbacks.
+The handlers are attached within `src/app.js` and are typical [express](https://expressjs.com/en/api.html) callbacks.
Each handler is defined in an file of the same name within `src`.
diff --git a/package.json b/package.json
index af498e6..bdb2c5f 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"private": true,
"name": "nextstrain.org",
- "main": "server.js",
+ "main": "server.mjs",
"author": "James Hadfield",
"license": "AGPL-3.0-only",
"repository": "github:nextstrain/nextstrain.org",
@@ -14,7 +14,7 @@
"build": "./build.sh",
"lint": "eslint --ext .js,.jsx .",
"set-up": "npm run build",
- "server": "node server.js",
+ "server": "node server.mjs",
"start": "npm run server",
"smoke-test": "NODE_ENV=test ENV=dev jest ./test/smoke-test/auspice_client_requests.test.js",
"smoke-test:ci": "start-server-and-test server http://localhost:5000 smoke-test",
diff --git a/server.js b/server.mjs
similarity index 93%
rename from server.js
rename to server.mjs
index f5cd69e..869fc15 100644
--- a/server.js
+++ b/server.mjs
@@ -1,7 +1,7 @@
-const argparse = require('argparse');
-const http = require("http");
+import argparse from "argparse";
+import http from "http";
-const utils = require("./src/utils");
+import utils from "./src/utils.js";
const version = utils.getGitHash();
const nextstrainAbout = `
@@ -26,7 +26,8 @@ const port = process.env.PORT || 5000;
/* Import app after setting global.verbose so that calls to utils.verbose()
* respect our --verbose option as expected.
*/
-const app = require("./src/app");
+import app from "./src/app.mjs";
+
app.set("port", port);
const server = http.createServer(app)
diff --git a/src/app.js b/src/app.mjs
similarity index 91%
rename from src/app.js
rename to src/app.mjs
index 46ea5c1..17d4320 100644
--- a/src/app.js
+++ b/src/app.mjs
@@ -1,4 +1,12 @@
/* eslint no-console: off */
+
+/* XXX TODO: Remove creation of "require()" once we no longer have a mix of
+ * CommonJS and ES modules.
+ * -trs, 30 Sept 2021
+ */
+import {createRequire} from "module";
+const require = createRequire(import.meta.url);
+
const path = require("path");
const sslRedirect = require('heroku-ssl-redirect');
const nakedRedirect = require('express-naked-redirect');
@@ -20,8 +28,10 @@ const redirects = require("./redirects");
/* Path helpers for static assets, to make routes more readable.
*/
+const basePath = path.dirname(path.dirname((new URL(import.meta.url)).pathname));
+
const relativePath = (...subpath) =>
- path.join(__dirname, "..", ...subpath);
+ path.join(basePath, ...subpath);
const gatsbyAssetPath = (...subpath) =>
relativePath("static-site", "public", ...subpath);
@@ -32,7 +42,7 @@ const auspiceAssetPath = (...subpath) =>
/* BASIC APP SETUP */
// NOTE: order of app.get is first come first serve (https://stackoverflow.com/questions/32603818/order-of-router-precedence-in-express-js)
-const app = addAsync(express());
+export const app = addAsync(express());
app.locals.production = production;
@@ -131,4 +141,4 @@ function sendAuspiceHandler(req, res, next) {
);
}
-module.exports = app;
+export default app;
--
2.37.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment