@@ -9,9 +9,58 @@ Please see the [Command Line Options][] document for information about
9
9
different options and ways to run scripts with Node.js.
10
10
11
11
## Example
12
-
13
12
An example of a [ web server] [ ] written with Node.js which responds with
14
- ` 'Hello World' ` :
13
+ ` 'Hello World!' ` :
14
+
15
+ Commands displayed in this document are shown starting with ` $ ` or ` > `
16
+ to replicate how they would appear in a user's terminal.
17
+ Do not include the ` $ ` and ` > ` character they are there to
18
+ indicate the start of each command.
19
+
20
+ There are many tutorials and examples that follow this
21
+ convention: ` $ ` or ` > ` for commands run as a regular user, and ` # `
22
+ for commands that should be executed as an administrator.
23
+
24
+ Lines that don’t start with ` $ ` or ` > ` character are typically showing
25
+ the output of the previous command.
26
+
27
+ Firstly, make sure to have downloaded and installed Node.js.
28
+ See [ this guide] [ ] for further install information.
29
+
30
+ Now, create an empty project folder called ` projects ` , navigate into it:
31
+ Project folder can be named base on user's current project title but
32
+ this example will use ` projects ` as the project folder.
33
+
34
+ Linux and Mac:
35
+
36
+ ``` console
37
+ $ mkdir ~ /projects
38
+ $ cd ~ /projects
39
+ ```
40
+
41
+ Windows CMD:
42
+
43
+ ``` console
44
+ > mkdir %USERPROFILE%\p rojects
45
+ > cd %USERPROFILE%\p rojects
46
+ ```
47
+
48
+ Windows PowerShell:
49
+
50
+ ``` console
51
+ > mkdir $env :USERPROFILE\p rojects
52
+ > cd $env :USERPROFILE\p rojects
53
+ ```
54
+
55
+ Next, create a new source file in the ` projects `
56
+ folder and call it ` hello-world.js ` .
57
+
58
+ In Node.js it is considered good style to use
59
+ hyphens (` - ` ) or underscores (` _ ` ) to separate
60
+ multiple words in filenames.
61
+
62
+ Open ` hello-world.js ` in any preferred text editor and
63
+ paste in the following content.
15
64
16
65
``` js
17
66
const http = require (' http' );
@@ -22,23 +71,34 @@ const port = 3000;
22
71
const server = http .createServer ((req , res ) => {
23
72
res .statusCode = 200 ;
24
73
res .setHeader (' Content-Type' , ' text/plain' );
25
- res .end (' Hello World\n ' );
74
+ res .end (' Hello World! \n ' );
26
75
});
27
76
28
77
server .listen (port, hostname, () => {
29
78
console .log (` Server running at http://${ hostname} :${ port} /` );
30
79
});
31
80
```
32
81
33
- To run the server, put the code into a file called ` example.js ` and execute
34
- it with Node.js:
82
+ Save the file, go back to the terminal window enter the following command:
35
83
36
- ``` txt
37
- $ node example.js
38
- Server running at http://127.0.0.1:3000/
84
+ ``` console
85
+ $ node hello-world.js
39
86
```
40
87
88
+ An output like this should appear in the terminal to indicate Node.js
89
+ server is running:
90
+
91
+ ``` console
92
+ Server running at http://127.0.0.1:3000/
93
+ ````
94
+
95
+ Now, open any preferred web browser and visit `http://127.0.0.1:3000`.
96
+
97
+ If the browser displays the string `Hello, world!`, that indicates
98
+ the server is working.
99
+
41
100
Many of the examples in the documentation can be run similarly.
42
101
43
102
[Command Line Options]: cli.html#cli_command_line_options
44
103
[web server]: http.html
104
+ [this guide]: https://nodejs.org/en/download/package-manager/
0 commit comments