Node.js Foundations·Practice drills 7

Drill 17: Find the core line — Deploying a Node service

Point to the line that does the main work and explain why.

Intermediate 5 min read Lesson 175 of 300 Challenge included
What you will be able to do
  • Point to the line that does the main work and explain why.

Practice task

Point to the line that does the main work and explain why.

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

Point to the line that does the main work and explain why.

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 175 of 300 · 0% of course