{ "type": "module", "source": "doc/api/synopsis.md", "modules": [ { "textRaw": "Usage and example", "name": "usage_and_example", "introduced_in": "v0.10.0", "miscs": [ { "textRaw": "Usage", "name": "Usage", "introduced_in": "v0.10.0", "type": "misc", "desc": "

node [options] [V8 options] [script.js | -e \"script\" | - ] [arguments]

\n

Please see the Command-line options document for more information.

\n

Example

\n

An example of a web server written with Node.js which responds with\n'Hello, World!':

\n

Commands in this document start with $ or > to replicate how they would\nappear in a user's terminal. Do not include the $ and > characters. They are\nthere to show the start of each command.

\n

Lines that don’t start with $ or > character show the output of the previous\ncommand.

\n

First, make sure to have downloaded and installed Node.js. See\nInstalling Node.js via package manager for further install information.

\n

Now, create an empty project folder called projects, then navigate into it.

\n

Linux and Mac:

\n
$ mkdir ~/projects\n$ cd ~/projects\n
\n

Windows CMD:

\n
> mkdir %USERPROFILE%\\projects\n> cd %USERPROFILE%\\projects\n
\n

Windows PowerShell:

\n
> mkdir $env:USERPROFILE\\projects\n> cd $env:USERPROFILE\\projects\n
\n

Next, create a new source file in the projects\nfolder and call it hello-world.js.

\n

Open hello-world.js in any preferred text editor and\npaste in the following content:

\n
const http = require('http');\n\nconst hostname = '127.0.0.1';\nconst port = 3000;\n\nconst server = http.createServer((req, res) => {\n  res.statusCode = 200;\n  res.setHeader('Content-Type', 'text/plain');\n  res.end('Hello, World!\\n');\n});\n\nserver.listen(port, hostname, () => {\n  console.log(`Server running at http://${hostname}:${port}/`);\n});\n
\n

Save the file, go back to the terminal window, and enter the following command:

\n
$ node hello-world.js\n
\n

Output like this should appear in the terminal:

\n
Server running at http://127.0.0.1:3000/\n
\n

Now, open any preferred web browser and visit http://127.0.0.1:3000.

\n

If the browser displays the string Hello, World!, that indicates\nthe server is working.

" } ], "type": "module", "displayName": "Usage and example" } ] }