11cb0ef41Sopenharmony_ci# Usage and example 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci## Usage 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0--> 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!--type=misc--> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci`node [options] [V8 options] [script.js | -e "script" | - ] [arguments]` 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciPlease see the [Command-line options][] document for more information. 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci## Example 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciAn example of a [web server][] written with Node.js which responds with 161cb0ef41Sopenharmony_ci`'Hello, World!'`: 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciCommands in this document start with `$` or `>` to replicate how they would 191cb0ef41Sopenharmony_ciappear in a user's terminal. Do not include the `$` and `>` characters. They are 201cb0ef41Sopenharmony_cithere to show the start of each command. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciLines that don't start with `$` or `>` character show the output of the previous 231cb0ef41Sopenharmony_cicommand. 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciFirst, make sure to have downloaded and installed Node.js. See 261cb0ef41Sopenharmony_ci[Installing Node.js via package manager][] for further install information. 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciNow, create an empty project folder called `projects`, then navigate into it. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciLinux and Mac: 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci```console 331cb0ef41Sopenharmony_ci$ mkdir ~/projects 341cb0ef41Sopenharmony_ci$ cd ~/projects 351cb0ef41Sopenharmony_ci``` 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciWindows CMD: 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci```console 401cb0ef41Sopenharmony_ci> mkdir %USERPROFILE%\projects 411cb0ef41Sopenharmony_ci> cd %USERPROFILE%\projects 421cb0ef41Sopenharmony_ci``` 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciWindows PowerShell: 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci```console 471cb0ef41Sopenharmony_ci> mkdir $env:USERPROFILE\projects 481cb0ef41Sopenharmony_ci> cd $env:USERPROFILE\projects 491cb0ef41Sopenharmony_ci``` 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ciNext, create a new source file in the `projects` 521cb0ef41Sopenharmony_cifolder and call it `hello-world.js`. 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciOpen `hello-world.js` in any preferred text editor and 551cb0ef41Sopenharmony_cipaste in the following content: 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci```js 581cb0ef41Sopenharmony_ciconst http = require('node:http'); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciconst hostname = '127.0.0.1'; 611cb0ef41Sopenharmony_ciconst port = 3000; 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciconst server = http.createServer((req, res) => { 641cb0ef41Sopenharmony_ci res.statusCode = 200; 651cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/plain'); 661cb0ef41Sopenharmony_ci res.end('Hello, World!\n'); 671cb0ef41Sopenharmony_ci}); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciserver.listen(port, hostname, () => { 701cb0ef41Sopenharmony_ci console.log(`Server running at http://${hostname}:${port}/`); 711cb0ef41Sopenharmony_ci}); 721cb0ef41Sopenharmony_ci``` 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ciSave the file, go back to the terminal window, and enter the following command: 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci```console 771cb0ef41Sopenharmony_ci$ node hello-world.js 781cb0ef41Sopenharmony_ci``` 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ciOutput like this should appear in the terminal: 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci```console 831cb0ef41Sopenharmony_ciServer running at http://127.0.0.1:3000/ 841cb0ef41Sopenharmony_ci``` 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ciNow, open any preferred web browser and visit `http://127.0.0.1:3000`. 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ciIf the browser displays the string `Hello, World!`, that indicates 891cb0ef41Sopenharmony_cithe server is working. 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci[Command-line options]: cli.md#options 921cb0ef41Sopenharmony_ci[Installing Node.js via package manager]: https://nodejs.org/en/download/package-manager/ 931cb0ef41Sopenharmony_ci[web server]: http.md 94