The runtime and modules
Node runs JavaScript outside the browser and gives it files, processes and modules.
What you will be able to do
- Use import and export with "type": "module" in package.json.
- Read configuration from environment variables, never hard-code secrets.
- Keep startup code separate from testable module code.
In plain English
Node uses an event loop and non-blocking I/O. ES modules are the modern default. The package.json file records dependencies and scripts; the engines field documents the runtime you support.
import { createServer } from 'node:http'
const port = Number(process.env.PORT || 3000)
const server = createServer((request, response) => {
response.writeHead(200, { 'content-type': 'application/json' })
response.end(JSON.stringify({ ok: true, path: request.url }))
})
server.listen(port, () => {
console.log('Listening on ' + port)
})Your turn
EasyRead a port safely
Read PORT from the environment and fall back to 3000. Print the value as a number.
JS · starter
const port = process.env.PORTCheck your understanding before moving on.
Knowledge checkQuestion 1
What does the engines field in package.json communicate?
Select an answer to continue
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 inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.