{ "type": "module", "source": "doc/api/synopsis.md", "modules": [ { "textRaw": "Usage & Example", "name": "usage_&_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]
Please see the Command Line Options document for more information.
\nAn example of a web server written with Node.js which responds with\n'Hello, World!'
:
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.
Lines that don’t start with $
or >
character show the output of the previous\ncommand.
First, make sure to have downloaded and installed Node.js. See this guide\nfor further install information.
\nNow, create an empty project folder called projects
, then navigate into it.
Linux and Mac:
\n$ mkdir ~/projects\n$ cd ~/projects\n
\nWindows CMD:
\n> mkdir %USERPROFILE%\\projects\n> cd %USERPROFILE%\\projects\n
\nWindows PowerShell:
\n> mkdir $env:USERPROFILE\\projects\n> cd $env:USERPROFILE\\projects\n
\nNext, create a new source file in the projects
\nfolder and call it hello-world.js
.
Open hello-world.js
in any preferred text editor and\npaste in the following content:
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
\nSave the file, go back to the terminal window, and enter the following command:
\n$ node hello-world.js\n
\nOutput like this should appear in the terminal:
\nServer running at http://127.0.0.1:3000/\n
\nNow, open any preferred web browser and visit http://127.0.0.1:3000
.
If the browser displays the string Hello, World!
, that indicates\nthe server is working.