0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 15:06:33 +01:00
nodejs/doc/api/synopsis.md

44 lines
993 B
Markdown
Raw Normal View History

# Usage
2012-02-27 20:09:34 +01:00
<!--type=misc-->
2010-10-28 14:18:16 +02:00
`node [options] [v8 options] [script.js | -e "script" | - ] [arguments]`
Please see the [Command Line Options][] document for information about
different options and ways to run scripts with Node.js.
## Example
An example of a [web server][] written with Node.js which responds with
`'Hello World'`:
2010-10-28 14:18:16 +02:00
```js
const http = require('http');
2010-10-28 14:18:16 +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
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
```
2010-10-28 14:18:16 +02:00
To run the server, put the code into a file called `example.js` and execute
it with Node.js:
2010-10-28 14:18:16 +02:00
```txt
$ node example.js
Server running at http://127.0.0.1:3000/
```
2010-10-28 14:18:16 +02:00
All of the examples in the documentation can be run similarly.
[Command Line Options]: cli.html#cli_command_line_options
[web server]: http.html