4c90abd16584a8177231406ccad9f605135bb004
[webi-installers/.git] / node / README.md
1 ---
2 title: Node.js
3 homepage: https://nodejs.org
4 tagline: |
5   Node.jsĀ® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
6 description: |
7   Node is great for simple, snappy HTTP(S) servers, and for stitching APIs together with minimal fuss or muss.
8
9   Installing node via webi will:
10
11     - pick a compatible version from the [Node Releases API](https://nodejs.org/dist/index.tab)
12     - download and unpack to `$HOME/.local/opt/node/`
13     - update your `PATH` in `$HOME/.config/envman/PATH.env`
14     - run `npm config set scripts-prepend-node-path=true`
15       - (prevents conflicts with other installed node versions)
16     - absolutely leave system file permisions alone
17       - (no dreaded `sudo npm` permission errors)
18 ---
19
20 Hello World
21
22 ```bash
23 node -e 'console.log("Hello, World!")'
24 > Hello, World!
25 ```
26
27 A Simple Web Server
28
29 `server.js`:
30
31 ```bash
32 var http = require('http');
33 var app = function (req, res) {
34   res.end('Hello, World!');
35 };
36 http.createServer(app).listen(8080, function () {
37   console.info('Listening on', this.address());
38 });
39 ```
40
41 ```bash
42 node server.js
43 ```
44
45 An Express App
46
47 ```bash
48 mkdir my-server
49 pushd my-server
50 npm init
51 npm install --save express
52 ```
53
54 `app.js`:
55
56 ```js
57 'use strict';
58
59 var express = require('express');
60 var app = express();
61
62 app.use('/', function (req, res, next) {
63   res.end("Hello, World!");
64 });
65
66 module.exports = app;
67 ```
68
69 `server.js`:
70
71 ```js
72 'use strict';
73
74 var http = require('http');
75 var app = require('./app.js');
76
77 http.createServer(app).listen(8080, function () {
78   console.info('Listening on', this.address());
79 });
80 ```
81
82 ```bash
83 npm start
84 ```