File tree Expand file tree Collapse file tree 2 files changed +23
-9
lines changed Expand file tree Collapse file tree 2 files changed +23
-9
lines changed Original file line number Diff line number Diff line change 11@// NB(chrisdickinson): if you move this file, be sure to update tools/doc/html.js to
22@// point at the new location.
33* [ About these Docs] ( documentation.html )
4- * [ Synopsis ] ( synopsis.html )
4+ * [ Usage & Example ] ( synopsis.html )
55* [ Assertion Testing] ( assert.html )
66* [ Buffer] ( buffer.html )
77* [ C/C++ Addons] ( addons.html )
Original file line number Diff line number Diff line change 1- # Synopsis
1+ # Usage
22
33<!-- type=misc-->
44
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+
512An example of a [ web server] [ ] written with Node.js which responds with
613` 'Hello World' ` :
714
815``` js
916const http = require (' http' );
1017
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+ });
1526
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+ });
1730```
1831
1932To 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:
2134
2235```
2336$ node example.js
24- Server running at http://127.0.0.1:8124 /
37+ Server running at http://127.0.0.1:3000 /
2538```
2639
2740All of the examples in the documentation can be run similarly.
2841
42+ [ Command Line Options ] : cli.html#cli_command_line_options
2943[ web server ] : http.html
You can’t perform that action at this time.
0 commit comments