<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marco</title>
    <description>The latest articles on DEV Community by Marco (@markuz899).</description>
    <link>https://dev.to/markuz899</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1125555%2Fe4d3da02-cd87-49cc-9dec-48188da8228a.jpeg</url>
      <title>DEV Community: Marco</title>
      <link>https://dev.to/markuz899</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/markuz899"/>
    <language>en</language>
    <item>
      <title>Nextjs custom server with fastify</title>
      <dc:creator>Marco</dc:creator>
      <pubDate>Sat, 29 Jun 2024 10:49:23 +0000</pubDate>
      <link>https://dev.to/markuz899/nextjs-custom-server-with-fastify-4gfe</link>
      <guid>https://dev.to/markuz899/nextjs-custom-server-with-fastify-4gfe</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrs9zyb3sk4j17h7li0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrs9zyb3sk4j17h7li0d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React server-side rendering support for Fastify with Next.js framework&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this post we will create a custom server to start an app in nextjs with fastify underneath.&lt;/p&gt;

&lt;p&gt;Let's create a new app in nextjs with npx&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's proceed with the installation of the dependencies&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install fastify @fastify/static fastify-env dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;First let's create an .env file at the root&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PORT=6100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;On the root of the project, so to speak, next to the src folder we are going to create a new folder called &lt;strong&gt;server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside we create an index.js file&lt;br&gt;
The content will look like this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require("dotenv").config();
const fastify = require("fastify")();
const fastifyStatic = require("@fastify/static");
const path = require("path");
const next = require("next");

const dev = process.env.NODE_ENV !== "production";
const host = process.env.HOST || "localhost";
const port = parseInt(process.env.PORT || "6100");

const app = next({ dev });
const handle = app.getRequestHandler();

(async () =&amp;gt; {
  try {
    await app.prepare();

    fastify.register(fastifyStatic, {
      root: path.join(__dirname, "..", "public", "static"),
      prefix: "/static/",
    });

    fastify.all("/*", (req, reply) =&amp;gt; {
      reply.hijack();
      handle(req.raw, reply.raw)
        .then(() =&amp;gt; {
          reply.raw.end();
        })
        .catch((err) =&amp;gt; {
          fastify.log.error(err);
          reply.raw.writeHead(500);
          reply.raw.end("Internal Server Error");
        });
    });

    await fastify.listen({ port, host });

    console.log(`Fastify server ready on port: ${port}`);
  } catch (err) {
    console.log(err);
    process.exit(1);
  }
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is the package.json file with the related run and build scripts&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "nextjs_fastify",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev-next": "next dev",
    "dev": "node server/index.js",
    "prod": "cross-env NODE_ENV=production node ./server/index.js",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@fastify/static": "^7.0.4",
    "dotenv": "^16.4.5",
    "fastify": "^4.28.1",
    "fastify-env": "^2.2.0",
    "next": "^14.2.4",
    "react": "^18",
    "react-dom": "^18"
  },
  "devDependencies": {
    "@types/next": "^9.0.0",
    "@types/node": "^20.14.9",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "cross-env": "^7.0.3",
    "typescript": "^5"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now all we have to do is run the desired scripts and that's it &lt;/p&gt;

&lt;p&gt;To run the server in dev mode&lt;br&gt;
&lt;code&gt;npm run dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To run the server in production mode you will first need to create the build version&lt;br&gt;
&lt;code&gt;npm run build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Only after the build has been completed can we launch the server in production mode with&lt;br&gt;
&lt;code&gt;npm run prod&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And tadaan we will now have a client in nextjs with fastify under the hood 🚀&lt;/p&gt;

&lt;p&gt;Thanks for reading&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>fastify</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
