|
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 |
0 commit comments