0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 15:06:33 +01:00
nodejs/doc/api/synopsis.md
Rich Trott fe4d53df51 doc: move Usage and Example to same header level
Having Example under Usage in synopsis.md is misleading. That suggests
that the examples will be examples of the CLI usage, but the example
section is mostly about writing a simple web server. Ideally, the Usage
section should be moved to cli.md and the Example section should
constitute a Getting Started or Quick Start page. But for now, make them
equals under a combined header so that the Table of Contents and the
header/layout of the page is not confusing or misleading.

PR-URL: https://github.com/nodejs/node/pull/28570
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-07-09 19:45:20 -07:00

92 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Usage & Example
## Usage
<!--introduced_in=v0.10.0-->
<!--type=misc-->
`node [options] [V8 options] [script.js | -e "script" | - ] [arguments]`
Please see the [Command Line Options][] document for more information.
## Example
An example of a [web server][] written with Node.js which responds with
`'Hello, World!'`:
Commands in this document start with `$` or `>` to replicate how they would
appear in a user's terminal. Do not include the `$` and `>` characters. They are
there to show the start of each command.
Lines that dont start with `$` or `>` character show the output of the previous
command.
First, make sure to have downloaded and installed Node.js. See [this guide][]
for further install information.
Now, create an empty project folder called `projects`, then navigate into it.
Linux and Mac:
```console
$ mkdir ~/projects
$ cd ~/projects
```
Windows CMD:
```console
> mkdir %USERPROFILE%\projects
> cd %USERPROFILE%\projects
```
Windows PowerShell:
```console
> mkdir $env:USERPROFILE\projects
> cd $env:USERPROFILE\projects
```
Next, create a new source file in the `projects`
folder and call it `hello-world.js`.
Open `hello-world.js` in any preferred text editor and
paste in the following content:
```js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
```
Save the file, go back to the terminal window, and enter the following command:
```console
$ node hello-world.js
```
Output like this should appear in the terminal:
```console
Server running at http://127.0.0.1:3000/
```
Now, open any preferred web browser and visit `http://127.0.0.1:3000`.
If the browser displays the string `Hello, World!`, that indicates
the server is working.
[Command Line Options]: cli.html#cli_command_line_options
[this guide]: https://nodejs.org/en/download/package-manager/
[web server]: http.html