Skip to content
Johnson Fu edited this page May 30, 2019 · 2 revisions

Quick Start

Basic Command

$ npm install express --save
$ npm install body-parser --save
$ npm install cookie-parser --save
$ npm install multer --save

How simple Node.js is ?

var express = require('express');
var app = express();
var fs = require("fs");

app.get('/listUsers', function (req, res) {
   fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
       console.log( data );
       res.end( data );
   });
})

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("Example app listening at http://%s:%s", host, port)

})

Version Checking

node -v
npm -version

nvm (node version manager) is a CLI to install different version of NodeJS

npm (node package manager) is a CLI for managing the node modues

Callback, Promise and Observable

  • callback - callbacks youtell the executing function what to do when the asynchronous task completes
  • promise - with promises the executing function returns a special object to you (the promise) and then you tell the promise what to do when the asynchronous task completes
  • observable - it's data stream…

promise

const promise = asyncFunc(function(resolve, reject) {.... });

promise.then(result => {
    console.log(result);
});

observable

const hello = Observable.create(function(observer) {
  observer.next('Hello');
  observer.next('World');
});

const subscribe = hello.subscribe(val => console.log(val));

From promise to observable, convert promise to observable

let Observable = Rx.Observable
let resultA, resultB, resultC

function addAsync (num1, num2) {
  const promise = fetch(`http://example.com?num1=${num1}&num2=${num2}`)
    .then(x => x.json())
  return Observable.fromPromise(promise)
}

addAsync(1, 2)
  .do(x => resultA = x)
  .flatMap(x => addAsync(x, 3))
  .do(x => resultB = x)
  .flatMap(x => addAsync(x, 4))
  .do(x => resultC = x)
  .subscribe(x => {
    console.log('total', x)
    conosle.log(resultA, resultB, resultC)
  })

compile ES6 to js

babel-node

https://medium.com/10coding/node-js-%E4%BD%BF%E7%94%A8-babel-%E5%81%9A-es6-%E9%96%8B%E7%99%BC-44b5b9e5f508

import and export

https://wohugb.gitbooks.io/ecmascript-6/docs/class.html

import {xxx, yyy} from './abc'

in abc file, export class xxx
export function yyy

import * as abc from './abc'
abc.xxx
abc.yyy()

import {xxx as a} from './abc'

Production Deployment

https://www.howtoing.com/install-pm2-to-run-nodejs-apps-on-linux-server

https://morning.work/page/2014-08/deploy-nodejs-production-app.html

https://code.kpman.cc/2015/09/12/%E5%BE%9Edeploy-node-js-%E5%B0%88%E6%A1%88%E6%89%80%E5%AD%B8/

Resources

Basic Tutorial

https://www.javatpoint.com/what-is-nodejs

Setup Environment

https://medium.com/@peterchang_82818/typescript-example-%E6%95%99%E5%AD%B8-tutorial-%E7%AF%84%E4%BE%8B-%E9%96%8B%E7%99%BC-%E6%95%99%E5%AD%B8-%E5%88%9D%E5%AD%B8%E8%80%85-%E7%92%B0%E5%A2%83-nodejs-javascript-react-js-3%E6%AD%A5-888fa8033fc7

Middleware Concept

https://andy6804tw.github.io/2017/12/27/middleware-tutorial/#2%E8%A8%AD%E5%AE%9A-body-parser-%E7%9A%84-middleware

Body Parser

https://github.com/expressjs/body-parser

Promise

https://itnext.io/javascript-promises-vs-rxjs-observables-de5309583ca2

https://medium.com/dev-bits/writing-neat-asynchronous-node-js-code-with-promises-32ed3a4fd098

https://andyyou.github.io/2017/06/27/js-promise/

Node in Production

https://www.pluralsight.com/tech-blog/running-nodejs-in-production

Node Express TypeScript API Tutorial

https://developer.okta.com/blog/2018/11/15/node-express-typescript

NodeJS API Backend + JWT + Angular Front End

https://www.toptal.com/angular/angular-6-jwt-authentication

Sources

https://github.com/quantificial/demo-node-simple

Express Boilerplate

https://github.com/w3tecch/express-typescript-boilerplate

Simple Restful Web Services

https://www.tutorialspoint.com/nodejs/nodejs_restful_api.htm

Securing Nodejs restful api

https://medium.freecodecamp.org/securing-node-js-restful-apis-with-json-web-tokens-9f811a92bb52

  • freemarker
  • thymeleaf
  • JMX (jconsole)
  • ZeroMQ
  • microk8s
  • multipass
  • pwsh (powershell)

Clone this wiki locally