For those who want to jump into learning Node.js with a minimum of noise, here is one way to approach it:
Note: This is easy on a Linux machine. (I used Ubuntu v. 16.04 LTS.)
First Install NVM
Note: you may need to install curl if it is not already installed. If you do, in a terminal window, type
sudo apt-get install curl
Then
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
Restart terminal, then check version with this:
nvm --version
Install Node
nvm install 6.11.4
(When installing a Node.js instance, nvm will also install a compatible npm version.)
Other commands: (from: https://www.sitepoint.com/quick-tip-multiple-versions-node-nvm/)
To check which npm version:
npm -v
To check which node versions are available
nvm ls-remote
Install the latest Node.js version
nvm install node
Uninstall any instance
nvm uninstall 4.8.4
Switch to node ver 4.8.4
nvm use 4.8.4
Switch to latest Node.js version
nvm use node
Check which versions you have installed
nvm ls
Create a custom alias
nvm alias awesome-version 4.8.4
use the alias version
nvm use awesome-version
Delete an alias
nvm unalias awesome-version
Simple node
Start node
node
Send text to the console
console.log('Aggie & Dave');
Exit node
process.exit()
or
.exit
or
Ctrl+C [enter]
Express
Open terminal
mkdir myApp
cd myApp
npm init (entry point: app.js)
npm install express --save (creates all necessary files)
Very simple app
Create a file called app.js
var express = require('express');
var app = express();
app.get("/", function(req, res) {
res.send("<h1>Home Page</h1>");
});
var server = app.listen(3000, function(){
console.log("Listening on port 3000");
});
In the terminal window, enter the following command to run the Node app (which launches the server):
node app.js
Open a browser. Navigate to: http://localhost:3000
App with multiple routes
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('<h1>Home</h1>');
});
app.get('/aggie', function(req, res) {
res.send('<h1>Aggie the dog!</h1>');
});
app.get('/julie', function(req, res) {
res.send('<h1>Julie is great.</h1>');
});
var server = app.listen(3000, function() {
console.log('Listening on port 3000');
});
Open a browser. Navigate to: http://localhost:3000