NodeJS simple HTTP server URL request handle

--

Its easy to set a simple server up, but what now. Now we need to handle requests. People are going to want to see something and we must show it to them.

So for this tutorial will use this as a base for setting up the server:

const http = require('http');
const port = process.env.PORT || 5000;
var server = http.createServer((req, res)=>
{
//Our code will go here
}.listen(port, ()=>{console.log('Server UP')});

This will set up the server on localhost:5000/ (5000 if there is no port set in the environment variable).

Now we will get the url the user requested with just one line.

var request = req.url

With this we can show what we want depending on what the user wanted, bellow is the final code.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response