Building a Basic HTTP Server

Let's begin by looking at the basic building blocks of a Node.js web application. The built-in http module is the core of this. However, from the following example, you will also appreciate how basic this can be.

Save the following code in a file called simple-server.js:

const http = require('http');
const server = http.createServer((request, response) =>
{
console.log('request starting...');
// respond
response.write('hello world!');
response.end();
});
server.listen(5000);
console.log('Server running at http://127.0.0.1:5000');

Use the simple-server.js file for your reference at Code/Lesson-2.

Now, let's run the file:

node simple-server.js

When we go to the browser and visit the URL in the example, this is what we get:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset