Server IP : 85.214.239.14 / Your IP : 3.16.67.134 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/cwd/usr/share/doc/nodejs/api/ |
Upload File : |
# Usage and 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 don't start with `$` or `>` character show the output of the previous command. First, make sure to have downloaded and installed Node.js. See [Installing Node.js via package manager][] for further install information. Now, create an empty project folder called `projects`, then navigate into it. Linux and Mac: ```bash mkdir ~/projects cd ~/projects ``` Windows CMD: ```powershell mkdir %USERPROFILE%\projects cd %USERPROFILE%\projects ``` Windows PowerShell: ```powershell 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('node: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. Then, in the terminal window, to run the `hello-world.js` file, enter: ```bash 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.md#options [Installing Node.js via package manager]: https://nodejs.org/en/download/package-manager/ [web server]: http.md