Skip to content

Commit afc8c79

Browse files
apal21Maledong
authored andcommitted
Update how-to-create-a-HTTP-server.md (#2206)
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.
1 parent b7f38e0 commit afc8c79

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

locale/en/knowledge/HTTP/servers/how-to-create-a-HTTP-server.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@ Making a simple HTTP server in Node.js has become the de facto 'hello world' for
1212

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

15-
var http = require('http');
16-
var requestListener = function (req, res) {
15+
const http = require('http');
16+
17+
const requestListener = function (req, res) {
1718
res.writeHead(200);
1819
res.end('Hello, World!\n');
1920
}
2021

21-
var server = http.createServer(requestListener);
22+
const server = http.createServer(requestListener);
2223
server.listen(8080);
2324

2425
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!
2526

27+
Also, from your terminal you should be able to get the response using curl:
28+
```
29+
curl localhost:8080
30+
```
31+
2632
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.
2733

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

0 commit comments

Comments
 (0)