File tree 2 files changed +23
-9
lines changed
2 files changed +23
-9
lines changed Original file line number Diff line number Diff line change 1
1
@// NB(chrisdickinson): if you move this file, be sure to update tools/doc/html.js to
2
2
@// point at the new location.
3
3
* [ About these Docs] ( documentation.html )
4
- * [ Synopsis ] ( synopsis.html )
4
+ * [ Usage & Example ] ( synopsis.html )
5
5
* [ Assertion Testing] ( assert.html )
6
6
* [ Buffer] ( buffer.html )
7
7
* [ C/C++ Addons] ( addons.html )
Original file line number Diff line number Diff line change 1
- # Synopsis
1
+ # Usage
2
2
3
3
<!-- type=misc-->
4
4
5
+ ` node [options] [v8 options] [script.js | -e "script"] [arguments] `
6
+
7
+ Please see the [ Command Line Options] [ ] document for information about
8
+ different options and ways to run scripts with Node.
9
+
10
+ ## Example
11
+
5
12
An example of a [ web server] [ ] written with Node.js which responds with
6
13
` 'Hello World' ` :
7
14
8
15
``` js
9
16
const http = require (' http' );
10
17
11
- http .createServer ( (request , response ) => {
12
- response .writeHead (200 , {' Content-Type' : ' text/plain' });
13
- response .end (' Hello World\n ' );
14
- }).listen (8124 );
18
+ const hostname = ' 127.0.0.1' ;
19
+ const port = 3000 ;
20
+
21
+ const server = http .createServer ((req , res ) => {
22
+ res .statusCode = 200 ;
23
+ res .setHeader (' Content-Type' , ' text/plain' );
24
+ res .end (' Hello World\n ' );
25
+ });
15
26
16
- console .log (' Server running at http://127.0.0.1:8124/' );
27
+ server .listen (port, hostname, () => {
28
+ console .log (` Server running at http://${ hostname} :${ port} /` );
29
+ });
17
30
```
18
31
19
32
To run the server, put the code into a file called ` example.js ` and execute
20
- it with the node program
33
+ it with Node.js:
21
34
22
35
```
23
36
$ node example.js
24
- Server running at http://127.0.0.1:8124 /
37
+ Server running at http://127.0.0.1:3000 /
25
38
```
26
39
27
40
All of the examples in the documentation can be run similarly.
28
41
42
+ [ Command Line Options ] : cli.html#cli_command_line_options
29
43
[ web server ] : http.html
You can’t perform that action at this time.
0 commit comments