Skip to content

Instantly share code, notes, and snippets.

@simonplend
Last active March 11, 2021 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonplend/37f860cb4d5e5760ff4a94137b4560fc to your computer and use it in GitHub Desktop.
Save simonplend/37f860cb4d5e5760ff4a94137b4560fc to your computer and use it in GitHub Desktop.
Code changes required to start the migration of an Express application to Fastify
diff --git a/api-after/package.json b/api-after/package.json
index 8efee6f..f51a9c8 100644
--- a/api-after/package.json
+++ b/api-after/package.json
@@ -11,6 +11,9 @@
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
- "express": "^4.17.1"
+ "express": "^4.17.1",
+ "fastify": "^3.13.0",
+ "fastify-express": "^0.3.2"
}
}
diff --git a/api-after/src/server.js b/api-after/src/server.js
index 22a692e..156553e 100644
--- a/api-after/src/server.js
+++ b/api-after/src/server.js
@@ -1,14 +1,17 @@
// src/server.js
-import express from "express";
+import Fastify from "fastify";
+import ExpressPlugin from "fastify-express";
import config from "./config.js";
import routes from "./routes.js";
-const app = express();
+const fastify = Fastify({
+ logger: true
+});
-app.use("/user", routes);
+await fastify.register(ExpressPlugin);
-const server = app.listen(3000, () => {
- console.log(`Example app listening at http://localhost:${server.address().port}`);
-});
+fastify.use("/user", routes);
+
+fastify.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment