2016-04-12 19:25:03 +02:00
|
|
|
# Usage
|
2012-02-27 20:09:34 +01:00
|
|
|
|
|
|
|
<!--type=misc-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-04-12 19:25:03 +02:00
|
|
|
`node [options] [v8 options] [script.js | -e "script"] [arguments]`
|
|
|
|
|
|
|
|
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
|
2015-11-28 00:30:32 +01:00
|
|
|
`'Hello World'`:
|
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');
|
|
|
|
res.end('Hello World\n');
|
|
|
|
});
|
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
|
|
|
|
|
|
|
To run the server, put the code into a file called `example.js` and execute
|
2016-04-12 19:25:03 +02:00
|
|
|
it with Node.js:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-07-09 07:13:09 +02:00
|
|
|
```txt
|
2016-01-21 23:21:22 +01:00
|
|
|
$ node example.js
|
2016-04-12 19:25:03 +02:00
|
|
|
Server running at http://127.0.0.1:3000/
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
All 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
|
2015-11-14 04:21:49 +01:00
|
|
|
[web server]: http.html
|