2016-04-12 19:25:03 +02:00
|
|
|
|
# Usage
|
2012-02-27 20:09:34 +01:00
|
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
|
<!--introduced_in=v0.10.0-->
|
2012-02-27 20:09:34 +01:00
|
|
|
|
<!--type=misc-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2017-11-17 06:41:14 +01:00
|
|
|
|
`node [options] [V8 options] [script.js | -e "script" | - ] [arguments]`
|
2016-04-12 19:25:03 +02:00
|
|
|
|
|
|
|
|
|
Please see the [Command Line Options][] document for information about
|
2016-04-29 19:29:36 +02:00
|
|
|
|
different options and ways to run scripts with Node.js.
|
2016-04-12 19:25:03 +02:00
|
|
|
|
|
|
|
|
|
## Example
|
2015-11-14 04:21:49 +01: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
|
|
|
|
|
|
|
|
|
Commands displayed in this document are shown starting with `$` or `>`
|
|
|
|
|
to replicate how they would appear in a user's terminal.
|
2018-10-30 08:49:18 +01:00
|
|
|
|
Do not include the `$` and `>` characters. They are there to
|
2018-01-04 11:17:01 +01:00
|
|
|
|
indicate the start of each command.
|
|
|
|
|
|
|
|
|
|
There are many tutorials and examples that follow this
|
|
|
|
|
convention: `$` or `>` for commands run as a regular user, and `#`
|
|
|
|
|
for commands that should be executed as an administrator.
|
|
|
|
|
|
|
|
|
|
Lines that don’t start with `$` or `>` character are typically showing
|
|
|
|
|
the output of the previous command.
|
|
|
|
|
|
|
|
|
|
Firstly, make sure to have downloaded and installed Node.js.
|
|
|
|
|
See [this guide][] for further install information.
|
|
|
|
|
|
2018-10-30 08:49:18 +01:00
|
|
|
|
Now, create an empty project folder called `projects`, then navigate into it.
|
|
|
|
|
The project folder can be named based on the user's current project title, but
|
2018-01-04 11:17:01 +01:00
|
|
|
|
this example will use `projects` as the project folder.
|
|
|
|
|
|
|
|
|
|
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`
|
|
|
|
|
folder and call it `hello-world.js`.
|
|
|
|
|
|
|
|
|
|
In Node.js it is considered good style to use
|
|
|
|
|
hyphens (`-`) or underscores (`_`) to separate
|
|
|
|
|
multiple words in filenames.
|
|
|
|
|
|
|
|
|
|
Open `hello-world.js` in any preferred text editor and
|
2018-10-30 08:49:18 +01:00
|
|
|
|
paste in the following content:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const http = require('http');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-04-12 19:25:03 +02: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 19:25:03 +02:00
|
|
|
|
});
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2016-04-12 19:25:03 +02:00
|
|
|
|
server.listen(port, hostname, () => {
|
|
|
|
|
console.log(`Server running at http://${hostname}:${port}/`);
|
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
2018-01-04 11:17:01 +01:00
|
|
|
|
Save the file, go back to the terminal window enter the following command:
|
2010-10-28 14:18:16 +02: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 14:18:16 +02:00
|
|
|
|
|
2018-01-04 11:17:01 +01:00
|
|
|
|
An output like this should appear in the terminal to indicate Node.js
|
|
|
|
|
server is running:
|
|
|
|
|
|
2018-04-29 13:16:44 +02: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.
|
|
|
|
|
|
2017-12-15 22:51:04 +01:00
|
|
|
|
Many of the examples in the documentation can be run similarly.
|
2015-11-14 04:21:49 +01:00
|
|
|
|
|
2016-04-12 19:25:03 +02:00
|
|
|
|
[Command Line Options]: cli.html#cli_command_line_options
|
2018-01-04 11:17:01 +01:00
|
|
|
|
[this guide]: https://nodejs.org/en/download/package-manager/
|
2018-11-27 20:49:21 +01:00
|
|
|
|
[web server]: http.html
|