JS 2 - Node.js Crash Course with Mongo

By Sheldon L Published at 2020-02-28 Updated at 2020-07-03


Install

Option 1 (npm)

# Install node.js and npm
sudo apt install nodejs
sudo apt install npm

node --version
npm --version
vim server.js
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
node server.js
node --inspect server.js
# Make a directory for global installations:
mkdir ~/.npm-global

# Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'

# Open or create a /.profile file and add this line:
export PATH=/.npm-global/bin:$PATH

# Back on the command line, update your system variables:
source ~/.bashrc
# OR
source ~/.profile
# install yarn:
sudo npm i yarn -g

Option 2 (yarn)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

# when nvm installed
sudo apt update && sudo apt install --no-install-recommends yarn

# vim ~/.bash_aliases
alias node=nodejs

yarn -v

# if yarn is not found, try vim ~/.bashrc:
export PATH="$PATH:/opt/yarn-[version]/bin

Trouble Shooting

Yarn - There appears to be trouble with your network connection. Retrying

Start a Project

new node.js projects and install packages

mkdir nodejs_crash_course
cd nodejs_crash_course

npm init
# a json file is created, can run `npm install` like python requirements

npm install uuid
npm install -D nodemon    # developement version
{
  "name": "nodejs_crash_course",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {               // change here
    "start": "node index",
    "dev": "nodemon index"
  },
  "author": "sheldon",
  "license": "ISC",
  "dependencies": {
    "uuid": "^7.0.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.2"
  }
}

npm run dev
touch index.js
vim index.js
console.log('hello')
node index
touch person.js
vim person.js
// Module wrapper function
(function (exports, require, module, __filename, __dirname) {

})

// console.log(__dirname, __filename)

class Person {
    constructor (name, age) {
        this.name = name;
        this.age = age;
    }

    greeting() {
        console.log(`Hello, I am ${this.name} and I am ${this.age}.`);
    }
}

module.exports = Person;
vim index.js
const Person = require('./person')

const person1 = new Person('John Doe', 30)

person1.greeting()
node index

Demos - path, fs, os, url, http

mkdir reference
cd reference
# see demos there
# https://github.com/sheldonldev/NodeJS_Crash_Course/tree/master/reference

Logger

cd <basedir>
touch logger.js
vim logger.js  # https://github.com/sheldonldev/NodeJS_Crash_Course/blob/master/logger.js
vim index.js   # https://github.com/sheldonldev/NodeJS_Crash_Course/blob/master/index.js

Http Server Example

cd <basedir>
mkdir public
touch public/index.html  # https://github.com/sheldonldev/NodeJS_Crash_Course/blob/master/public/404.html
touch public/about.html  # https://github.com/sheldonldev/NodeJS_Crash_Course/blob/master/public/about.html
touch public/404.html    # https://github.com/sheldonldev/NodeJS_Crash_Course/blob/master/public/404.html
vim index.js  # index.js

Deploy to heroku

heroku --version
git --version
node_modules
reference
logger.js
person.js
git init
git add .
git commit -m 'init'
heroku create
heroku git:remote -a radiant-shore-*****
git push heroku master
heroku open