{ "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]
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\nInstalling Node.js via package manager for further install information.
\nNow, create an empty project folder called projects
, then navigate into it.
Linux and Mac:
\nmkdir ~/projects\ncd ~/projects\n
\nWindows CMD:
\nmkdir %USERPROFILE%\\projects\ncd %USERPROFILE%\\projects\n
\nWindows PowerShell:
\nmkdir $env:USERPROFILE\\projects\ncd $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('node: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. Then, in the terminal window, to run the hello-world.js
file,\nenter:
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.