{ "type": "module", "source": "doc/api/synopsis.md", "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 information about\ndifferent options and ways to run scripts with Node.js.

\n

Example

\n

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

\n

Commands displayed in this document are shown starting with $ or >\nto replicate how they would appear in a user's terminal.\nDo not include the $ and > characters. They are there to\nindicate the start of each command.

\n

There are many tutorials and examples that follow this\nconvention: $ or > for commands run as a regular user, and #\nfor commands that should be executed as an administrator.

\n

Lines that don’t start with $ or > character are typically showing\nthe output of the previous command.

\n

Firstly, make sure to have downloaded and installed Node.js.\nSee this guide for further install information.

\n

Now, create an empty project folder called projects, then navigate into it.\nThe project folder can be named based on the user's current project title, but\nthis example will use projects as the project folder.

\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

In Node.js it is considered good style to use\nhyphens (-) or underscores (_) to separate\nmultiple words in filenames.

\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 enter the following command:

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

An output like this should appear in the terminal to indicate Node.js\nserver is running:

\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.

\n

Many of the examples in the documentation can be run similarly.

" } ] }