0
0
NextJSframework~20 mins

Self-hosting with Node.js in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Self-hosting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js server component when self-hosted with Node.js?

Consider this simple Next.js server component that fetches data and renders it. What will be the rendered output when accessed via a Node.js self-hosted server?

NextJS
import React from 'react';

export default async function Greeting() {
  const data = await Promise.resolve({ name: 'Alice' });
  return <h1>Hello, {data.name}!</h1>;
}
AError: Cannot use await in this context
B<h1>Hello, undefined!</h1>
C<h1>Hello, Alice!</h1>
D<h1>Hello, !</h1>
Attempts:
2 left
💡 Hint

Think about how Next.js server components handle async data fetching.

lifecycle
intermediate
2:00remaining
Which lifecycle behavior is true for Next.js API routes when self-hosted with Node.js?

When you self-host a Next.js API route on Node.js, which statement about its lifecycle is correct?

AThe API route runs once at server start and caches all responses.
BThe API route runs only once per user session.
CThe API route runs only on client-side navigation events.
DThe API route runs on every request, creating a new instance each time.
Attempts:
2 left
💡 Hint

Think about how serverless functions behave in a Node.js environment.

🔧 Debug
advanced
2:00remaining
Why does this self-hosted Next.js app fail to start with Node.js?

Examine the following server start script. Why does it fail with a syntax error?

NextJS
import { createServer } from 'http';
import next from 'next';

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

createServer((req, res) => {
  app.prepare().then(() => {
    handle(req, res);
  });
}).listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
ATypeError because 'app.prepare' is not a function.
BSyntaxError because 'import' cannot be used in CommonJS without 'type: module'.
CRuntime error because 'handle' is undefined.
DNo error; the code runs correctly.
Attempts:
2 left
💡 Hint

Check the Node.js module system compatibility with ES modules.

📝 Syntax
advanced
2:00remaining
Which option correctly configures a Next.js custom server with Express for self-hosting?

Choose the correct code snippet to create a custom Express server that handles Next.js pages.

A
const express = require('express');
const next = require('next');

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

app.prepare().then(() =&gt; {
  const server = express();
  server.all('*', (req, res) =&gt; handle(req, res));
  server.listen(3000);
});
B
import express from 'express';
import next from 'next';

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

app.prepare().then(() =&gt; {
  const server = express();
  server.get('/', (req, res) =&gt; handle(req, res));
  server.listen(3000);
});
C
import express from 'express';
import next from 'next';

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

app.prepare().then(() =&gt; {
  const server = express();
  server.use((req, res) =&gt; handle(req, res));
  server.listen(3000);
});
D
import express from 'express';
import next from 'next';

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

app.prepare().then(() =&gt; {
  const server = express();
  server.get('*', (req, res) =&gt; handle(req, res));
  server.listen(3000);
});
Attempts:
2 left
💡 Hint

Consider which HTTP methods and routes should be handled to serve all Next.js pages.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of self-hosting a Next.js app with Node.js over using Vercel's platform?

Choose the best explanation for why a developer might choose to self-host a Next.js app using Node.js instead of deploying on Vercel.

ASelf-hosting allows full control over the server environment and custom backend integrations.
BSelf-hosting automatically scales the app globally without configuration.
CSelf-hosting eliminates the need to write API routes or server components.
DSelf-hosting guarantees zero downtime without any additional setup.
Attempts:
2 left
💡 Hint

Think about flexibility and control versus managed services.