Resources & ToolsLesson 126Free preview
Node.js Server
This lesson is part of Node.js · All levels
Summary
This lesson explains how to create a basic HTTP server with built-in HTTP modules. You'll understand the request-response pattern, read the method, URL, and body of the request, and then send the response with the correct status code and header. This concept became the foundation before moving to a framework like Express. (Node.js)
Key points
- Http.createServer(callback) receives two arguments: req (IncomingMessage) and res (ServerResponse).
- Res.writeHead(status, headers) sets the status code and header; res.end() closes the response.
- Read the method via req.method and the URL via req.url to build a simple routing.
- Body request is read as a stream: collect the chunk through the event 'data' and 'end'.
- Always set Content-Type so that the client knows how to display the data.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.