Browse Source

doc: better example & synopsis

PR-URL: https://github.com/nodejs/node/pull/6167
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Robert Jefe Lindstädt <robert.lindstaedt@gmail.com>
v4.x
Jeremiah Senkpiel 9 years ago
committed by Myles Borins
parent
commit
401325f9e2
  1. 2
      doc/api/_toc.markdown
  2. 30
      doc/api/synopsis.markdown

2
doc/api/_toc.markdown

@ -1,7 +1,7 @@
@// NB(chrisdickinson): if you move this file, be sure to update tools/doc/html.js to @// NB(chrisdickinson): if you move this file, be sure to update tools/doc/html.js to
@// point at the new location. @// point at the new location.
* [About these Docs](documentation.html) * [About these Docs](documentation.html)
* [Synopsis](synopsis.html) * [Usage & Example](synopsis.html)
* [Assertion Testing](assert.html) * [Assertion Testing](assert.html)
* [Buffer](buffer.html) * [Buffer](buffer.html)
* [C/C++ Addons](addons.html) * [C/C++ Addons](addons.html)

30
doc/api/synopsis.markdown

@ -1,29 +1,43 @@
# Synopsis # Usage
<!--type=misc--> <!--type=misc-->
`node [options] [v8 options] [script.js | -e "script"] [arguments]`
Please see the [Command Line Options][] document for information about
different options and ways to run scripts with Node.
## Example
An example of a [web server][] written with Node.js which responds with An example of a [web server][] written with Node.js which responds with
`'Hello World'`: `'Hello World'`:
```js ```js
const http = require('http'); const http = require('http');
http.createServer( (request, response) => { const hostname = '127.0.0.1';
response.writeHead(200, {'Content-Type': 'text/plain'}); const port = 3000;
response.end('Hello World\n');
}).listen(8124); const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
console.log('Server running at http://127.0.0.1:8124/'); server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
``` ```
To run the server, put the code into a file called `example.js` and execute To run the server, put the code into a file called `example.js` and execute
it with the node program it with Node.js:
``` ```
$ node example.js $ node example.js
Server running at http://127.0.0.1:8124/ Server running at http://127.0.0.1:3000/
``` ```
All of the examples in the documentation can be run similarly. All of the examples in the documentation can be run similarly.
[Command Line Options]: cli.html#cli_command_line_options
[web server]: http.html [web server]: http.html

Loading…
Cancel
Save