Node.js Foundations·Practice drills 1

Drill 2: Teach it — Deploying a Node service

Explain the example to someone who has never seen it.

Intermediate 5 min read Lesson 25 of 300 Challenge included
What you will be able to do
  • Explain the example to someone who has never seen it.

Practice task

Explain the example to someone who has never seen it.

server.tsTypeScript
import { createServer } from 'node:http'

const port = Number(process.env.PORT ?? 3000)

const server = createServer((request, response) => {
  if (request.url === '/health') {
    response.writeHead(200, { 'content-type': 'application/json' })
    return response.end(JSON.stringify({ status: 'ok' }))
  }
  response.writeHead(404)
  response.end()
})

server.listen(port, () => {
  console.log('Listening on ' + port)
})

function shutdown(signal: string) {
  console.log('Received ' + signal + ', closing server')
  server.close(() => {
    console.log('Server closed')
    process.exit(0)
  })
  setTimeout(() => {
    console.error('Forced shutdown after timeout')
    process.exit(1)
  }, 10_000).unref()
}

process.on('SIGTERM', () => shutdown('SIGTERM'))
process.on('SIGINT', () => shutdown('SIGINT'))
Your turn

Write your answer

Easy

Explain the example to someone who has never seen it.

TEXT · starter
Your answer…
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 25 of 300 · 0% of course