2020-06-14 14:49:34 -07:00
|
|
|
# Usage and example
|
2019-07-05 16:35:19 -07:00
|
|
|
|
|
|
|
## Usage
|
2012-02-27 11:09:34 -08:00
|
|
|
|
2017-01-22 19:16:21 -08:00
|
|
|
<!--introduced_in=v0.10.0-->
|
2021-09-19 17:18:42 -07:00
|
|
|
|
2012-02-27 11:09:34 -08:00
|
|
|
<!--type=misc-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2017-11-16 21:41:14 -08:00
|
|
|
`node [options] [V8 options] [script.js | -e "script" | - ] [arguments]`
|
2016-04-12 13:25:03 -04:00
|
|
|
|
2020-09-14 17:28:27 -07:00
|
|
|
Please see the [Command-line options][] document for more information.
|
2016-04-12 13:25:03 -04:00
|
|
|
|
|
|
|
## Example
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2015-11-13 19:21:49 -08:00
|
|
|
An example of a [web server][] written with Node.js which responds with
|
2018-09-21 21:58:07 +02:00
|
|
|
`'Hello, World!'`:
|
2018-01-04 11:17:01 +01:00
|
|
|
|
2019-06-06 19:48:16 -07:00
|
|
|
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.
|
2018-01-04 11:17:01 +01:00
|
|
|
|
2022-05-17 21:04:51 +02:00
|
|
|
Lines that don't start with `$` or `>` character show the output of the previous
|
2019-06-06 19:48:16 -07:00
|
|
|
command.
|
2018-01-04 11:17:01 +01:00
|
|
|
|
2020-09-21 09:22:54 -07:00
|
|
|
First, make sure to have downloaded and installed Node.js. See
|
|
|
|
[Installing Node.js via package manager][] for further install information.
|
2018-01-04 11:17:01 +01:00
|
|
|
|
2018-10-30 18:49:18 +11:00
|
|
|
Now, create an empty project folder called `projects`, then navigate into it.
|
2018-01-04 11:17:01 +01:00
|
|
|
|
|
|
|
Linux and Mac:
|
|
|
|
|
|
|
|
```console
|
|
|
|
$ mkdir ~/projects
|
|
|
|
$ cd ~/projects
|
|
|
|
```
|
|
|
|
|
|
|
|
Windows CMD:
|
|
|
|
|
|
|
|
```console
|
|
|
|
> mkdir %USERPROFILE%\projects
|
|
|
|
> cd %USERPROFILE%\projects
|
|
|
|
```
|
|
|
|
|
|
|
|
Windows PowerShell:
|
|
|
|
|
|
|
|
```console
|
|
|
|
> mkdir $env:USERPROFILE\projects
|
|
|
|
> cd $env:USERPROFILE\projects
|
|
|
|
```
|
|
|
|
|
|
|
|
Next, create a new source file in the `projects`
|
2021-10-10 21:55:04 -07:00
|
|
|
folder and call it `hello-world.js`.
|
2018-01-04 11:17:01 +01:00
|
|
|
|
|
|
|
Open `hello-world.js` in any preferred text editor and
|
2018-10-30 18:49:18 +11:00
|
|
|
paste in the following content:
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2022-04-20 10:23:41 +02:00
|
|
|
const http = require('node:http');
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2016-04-12 13:25:03 -04:00
|
|
|
const hostname = '127.0.0.1';
|
|
|
|
const port = 3000;
|
|
|
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.statusCode = 200;
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
2018-09-21 21:58:07 +02:00
|
|
|
res.end('Hello, World!\n');
|
2016-04-12 13:25:03 -04:00
|
|
|
});
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2016-04-12 13:25:03 -04:00
|
|
|
server.listen(port, hostname, () => {
|
|
|
|
console.log(`Server running at http://${hostname}:${port}/`);
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2019-06-06 19:48:16 -07:00
|
|
|
Save the file, go back to the terminal window, and enter the following command:
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2018-01-04 11:17:01 +01:00
|
|
|
```console
|
|
|
|
$ node hello-world.js
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2019-06-06 19:48:16 -07:00
|
|
|
Output like this should appear in the terminal:
|
2018-01-04 11:17:01 +01:00
|
|
|
|
2018-04-29 14:16:44 +03:00
|
|
|
```console
|
|
|
|
Server running at http://127.0.0.1:3000/
|
|
|
|
```
|
2018-01-04 11:17:01 +01:00
|
|
|
|
|
|
|
Now, open any preferred web browser and visit `http://127.0.0.1:3000`.
|
|
|
|
|
2018-09-21 21:58:07 +02:00
|
|
|
If the browser displays the string `Hello, World!`, that indicates
|
2018-01-04 11:17:01 +01:00
|
|
|
the server is working.
|
|
|
|
|
2022-01-17 11:36:19 +01:00
|
|
|
[Command-line options]: cli.md#options
|
2020-09-21 09:22:54 -07:00
|
|
|
[Installing Node.js via package manager]: https://nodejs.org/en/download/package-manager/
|
2020-09-14 17:09:13 +02:00
|
|
|
[web server]: http.md
|