Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Replace express with connect #27045

Draft
wants to merge 11 commits into
base: next
Choose a base branch
from

Conversation

43081j
Copy link
Contributor

@43081j 43081j commented May 7, 2024

Migrates away from express, instead using connect and the underlying node server directly.

Both express and connect support the same middleware structure, which means most middleware packages work for both and have remained the same in this change.

A few notes:

  • Express' router has been replaced by basic connect mount points (though the two are not equivalent exactly)
  • Express static has been replaced by sirv

This trims a large sub-tree of dependencies from our packages which express was pulling in. We in fact use very little of the functionality express gives us, only really making use of the connect-style API anyway.

I've left a few TODOs around the code while this is a draft to help guide reviewers until it is done (if ever)

Express static

Replacing this with sirv means it isn't as simple as mounting real paths at aliased paths, since sirv takes a single directory as its source.

Due to this, we give it process.cwd() and mutate req.url to be the path relative to cwd.

Express router

We previously exposed router to managers and a few other middleware factories. These now consume an app (a connect Server).

A few main differences between the "full fat" router and basic mounts:

  • No params, parsed query strings, etc
  • Matches the prefix rather than the whole route
    • This means app('/foo', fn) would match /foo/bar too, and fn would receive {url: '/bar'} (relative to the mount point)

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

Run the sandbox and observe that it all works as expected

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

cc @ndelangen

Migrates away from express, instead using connect and the underlying
node server directly.

Both express and connect support the same middleware structure, which
means most middleware packages work for both and have remained the same
in this change.

A few notes:

- Express' router has been replaced by basic connect mount points
(though the two are not equivalent exactly)
- Express static has been replaced by sirv

This trims a large sub-tree of dependencies from our packages which
express was pulling in. We in fact use very little of the functionality
express gives us, only really making use of the connect-style API
anyway.
Copy link

nx-cloud bot commented May 7, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 66d27f8. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@ndelangen ndelangen changed the title (WIP) feat: migrate from express to connect Core: Replace express with connect May 7, 2024
@ndelangen ndelangen self-assigned this May 7, 2024
@ndelangen ndelangen self-requested a review May 7, 2024 12:42
@@ -114,7 +114,7 @@ export const bail: WebpackBuilder['bail'] = async () => {
const starter: StarterFunction = async function* starterGeneratorFn({
startTime,
options,
router,
app,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way we can make this backwards compatible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be difficult since router is basically the {get, post} shape etc

maybe we could have a wrapper around the connect server which does these 🤔

something like:

const router = {
  get(path, callback) {
    app(path, (req, res, next) => {
      // check the method is `GET` here and do nothing if it isn't
      callback(req, res, next);
    });
  },
  // ...
};

although keep in mind path would be treated as a prefix rather than a route pattern (i.e. /foo would capture /foo/bar, and there's no such thing as /foo/:someParam anymore, i.e. params).

@43081j
Copy link
Contributor Author

43081j commented May 7, 2024

@ndelangen got the tests passing for you FYI 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants