DevAcademy
LearnNode.jsCreating a Server
IntermediateNode.js

Creating a Server

Build a slightly more complete HTTP server: routing by URL, and returning JSON.

Reading Time

14 min

Lesson

Lesson 17 of 34

Manual Routing

Without a framework, "routing" just means writing conditional logic that inspects req.url and req.method, and responds differently based on what it finds.

A Server With Basic Routes

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Reading a Request Body

The request body arrives as a stream of Buffer chunks, not as a ready-made value — you have to collect and parse it yourself when building directly on http, which frameworks handle for you automatically.

Parsing a JSON Request Body

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Common HTTP Status Codes

CodeMeaning
200OK
201Created
400Bad Request
404Not Found
500Internal Server Error

Best Practice

Seeing how much manual work goes into routing and body parsing with raw http is exactly why real projects use a framework like Express — it replaces all of this boilerplate with a few lines of declarative route definitions.

Interview Questions

Quick Quiz

1. How does a raw http server determine which route to serve?

2. How does a raw http server body arrive?

3. What HTTP status code conventionally indicates a resource was successfully created?