Skip to content

Commit

Permalink
Update how-to-create-a-HTTP-server.md (#2206)
Browse files Browse the repository at this point in the history
In accordance with issue #1977, I have updated the Article how-to-create-a-HTTP-server.md with the following changes:

1. Used `const` instead of `var`.
2. Added CURL example.
  • Loading branch information
apal21 authored and Maledong committed Apr 27, 2019
1 parent b7f38e0 commit afc8c79
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions locale/en/knowledge/HTTP/servers/how-to-create-a-HTTP-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ Making a simple HTTP server in Node.js has become the de facto 'hello world' for

Let's take a look at a very simple example:

var http = require('http');
var requestListener = function (req, res) {
const http = require('http');

const requestListener = function (req, res) {
res.writeHead(200);
res.end('Hello, World!\n');
}

var server = http.createServer(requestListener);
const server = http.createServer(requestListener);
server.listen(8080);

Save this in a file called `server.js` - run `node server.js`, and your program will hang there... it's waiting for connections to respond to, so you'll have to give it one if you want to see it do anything. Try opening up a browser, and typing `localhost:8080` into the location bar. If everything has been set up correctly, you should see your server saying hello!

Also, from your terminal you should be able to get the response using curl:
```
curl localhost:8080
```

Let's take a more in-depth look at what the above code is doing. First, a function is defined called `requestListener` that takes a request object and a response object as parameters.

The request object contains things such as the requested URL, but in this example we ignore it and always return "Hello World".
Expand Down

0 comments on commit afc8c79

Please sign in to comment.