Top Banner
May The Node.js Be With You @dalibor_gogic
42

May The Nodejs Be With You

Jan 17, 2017

Download

Education

Dalibor Gogic
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: May The Nodejs Be With You

May The Node.js Be With You@dalibor_gogic

Page 2: May The Nodejs Be With You

Node.js®

Is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

https://nodejs.org/

$ sudo apt-get update$ sudo apt-get install nodejs$ sudo apt-get install npm

Page 3: May The Nodejs Be With You

Optional: install build tools

To compile and install native addons from npm you may also need to

install build tools:

$ sudo apt-get install build-essential libssl-dev

Page 4: May The Nodejs Be With You

To confirm that you have Node.js installed:

$ node -v

> v7.2.0

To confirm that you have npm installed:

$ npm -v

> v4.0.3

Page 5: May The Nodejs Be With You

Use a Node.js Version Manager

Since you probably already have node, the easiest way to install n

is through npm:

$ npm i -g n

https://github.com/tj/n/

Page 6: May The Nodejs Be With You

Installing/Activating Versions

Use or install the latest official release:

$ n latest

Stable:

$ n stable

Latest:

$ n lts

Woot! Nightly:

$ NODE_MIRROR=https://nodejs.org/download/test/ n \

v8.0.0-test201610181280ed2be7

Page 7: May The Nodejs Be With You

npm

Package manager for JavaScript. Find, share, and reuse packages of code

from hundreds of thousands of developers — and assemble them in new

ways.

https://www.npmjs.com/

Nov/25 358842 packages

Avg Growth 470/day

Page 8: May The Nodejs Be With You

The best way to manage locally installed npm packages is to create a

package.json file.

As a bare minimum, a package.json must have:

{

"name": "foo",

"version": "1.0.0"

}

To create a package.json:

$ npm init -y

Page 9: May The Nodejs Be With You

// package.json{

"name": foo",

"version": "1.0.0",

"description": "",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"keywords": [],

"author": "",

"license": "ISC"

}

Page 10: May The Nodejs Be With You

Optional config options for the init command. Some useful ones:

$ npm set init.author.email "[email protected]"

$ npm set init.author.name "Dalibor Gogic"

$ npm set init.license "MIT"

$ npm help json

Page 11: May The Nodejs Be With You

Install a package

$ npm i (with no args, in package dir)

$ npm i [<@scope>/]<name>

$ npm i [<@scope>/]<name>@<tag>

$ npm i [<@scope>/]<name>@<version>

$ npm i [<@scope>/]<name>@<version range>

$ npm i <tarball file>

$ npm i <tarball url>

$ npm i <folder>

common options: [-S|--save|-D|--save-dev|-O|--save-optional] \ [-E|--save-exact]

[--dry-run]

https://docs.npmjs.com/

Page 12: May The Nodejs Be With You

EditorConfig

Helps developers define and maintain consistent coding styles between

different editors and IDEs.

// .editorconfig

root = true

[*]

indent_style = space

indent_size = 2

end_of_line = lf

charset = utf-8

trim_trailing_whitespace = true

insert_final_newline = true

[*.md]

trim_trailing_whitespace = false

Page 13: May The Nodejs Be With You

EditorConfig:

http://editorconfig.org/

Sublime Text PlugIn:

https://github.com/sindresorhus/editorconfig-sublime/

Atom PlugIn:

https://github.com/sindresorhus/atom-editorconfig/

Page 14: May The Nodejs Be With You

Git

Git allows groups of people to work on the same documents (often code)

at the same time, and without stepping on each other's toes.

$ sudo apt-get update

$ sudo apt-get install git

$ git --version

Git version 2.7.4

https://git-scm.com/

Page 15: May The Nodejs Be With You

First-Time Git Setup:

$ git config --global user.name "dalibor"

$ git config --global user.email [email protected]

$ git config --global core.editor "subl -n -w"

Page 16: May The Nodejs Be With You

$ git init

Initialized empty Git repository in /home/dalibor/foo/.git/

$ git status

On branch master

Initial commit

Untracked files:

(use "git add <file>..." to include in what will be committed

package.json

nothing added to commit but untracked files present (use "git add" to

track)

Page 17: May The Nodejs Be With You

$ git add package.json

$ git commit -m "initial package.json"

$ git status

[master (root-commit) 87568a5] initial package.json

1 file changed, 12 insertions(+)

create mode 100644 package.json

Docs: https://git-scm.com/doc

TryGit: https://try.github.io

Page 18: May The Nodejs Be With You

$ touch app.js

// app.js

'use strict'

const app = require('express')()

app.get('/', (req, res) => {

res.send('Hello World!')

})

app.listen(3000, () => {

console.log('Access at [::1]:3000')

})

Page 19: May The Nodejs Be With You

ESLint

The pluggable linting utility for JavaScript.

http://eslint.org/

$ npm i -g eslint

$ eslint --init

$ eslint --env browser, node app.js/foo/app.js

1:2 error Comments should not begin with a lowercase character capitalized-comments

2:21 error Strings must use doublequote quotes

...

Standard - ESLint Shareable Config

https://github.com/feross/eslint-config-standard/

Page 20: May The Nodejs Be With You

// package.json

{

"name": "foo",

"version": "1.0.0",

"scripts": {

"dev": "node app.js",

"start": "NODE_ENV=production node app.js",

"lint": "eslint --env browser, node app.js",

"test": "echo \"Error: no test specified\" && exit 1"

},

"dependencies": {

"express": "^4.14.0"

}

}

$ npm run lint

Page 21: May The Nodejs Be With You

// package.json

{

"name": "foo",

"version": "1.0.0",

"scripts": {

"predev": "npm run lint",

"dev": "node app.js",

"start": "NODE_ENV=production node app.js",

"lint": "eslint --env browser, node app.js",

"test": "echo \"Error: no test specified\" && exit 1"

},

"dependencies": {

"express": "^4.14.0"

}

}

$ npm run dev

Page 22: May The Nodejs Be With You

Nodemon

Monitor for any changes in your node.js application and automatically

restart the server.

https://github.com/remy/nodemon/

$ npm i -g nodemon

Or from directory that contains package.json

$ npm i -D nodemon

Page 23: May The Nodejs Be With You

// package.json

{

"name": "foo",

"version": "1.0.0",

"scripts": {

"predev": "npm run lint",

"dev": "nodemon app.js",

"start": "NODE_ENV=production node app.js",

"lint": "eslint --env browser, node app.js",

"test": "echo \"Error: no test specified\" && exit 1"

},

"dependencies": {

"express": "^4.14.0"

},

"devDependencies": {

"nodemon": "^1.11.0"

}

}

$ npm run dev

Page 24: May The Nodejs Be With You

Pug

High performance template engine heavily influenced by Haml and implemented with JavaScript

for Node.js and browsers.

> This project was formerly known as "Jade."

https://github.com/pugjs/pug/

$ npm i -S pug

Page 25: May The Nodejs Be With You

// app.js

'use strict'

const app = require('express')()

app.set('view engine', 'pug')

app.get('/', (req, res) => {

res.render('index', {

title: 'Pug'

})

})

app.listen(3000, () => {

console.log('Access at [::1]:3000')

})

Page 26: May The Nodejs Be With You

// views/index.pug

doctype html

html(lang="en")

head

meta(charset="utf-8")

title= title

body

h1 Hello World

// index.html

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Pug</title>

</head>

<body>

<h1>Hello World</h1>

</body>

</html>

Page 27: May The Nodejs Be With You

Stylus

Expressive, dynamic, robust, feature-rich CSS preprocessor

http://stylus-lang.com/

$ npm i -D stylus

Page 28: May The Nodejs Be With You

Variables

// style.styl

font-size = 14px

body

font font-size Arial, sans-serif

// style.css

body {

font: 14px Arial, sans-serif;

}

Page 29: May The Nodejs Be With You

Parent Reference

// style.styl

input

color #a7a7a7

&:hover

color white

// style.css

input {

color: #a7a7a7;

}

input:hover {

color: white

}

Page 30: May The Nodejs Be With You

Functions

// style.styl

add(a, b)

a + b

body

padding add(10px, 5)

// style.css

body {

padding: 15px;

}

Page 31: May The Nodejs Be With You

CONDITIONALS if / else if / else

// style.styl

overload-padding = true

if overload-padding

padding(y, x)

margin y x

body

padding 5px 10px

// style.css

body {

margin: 5px 10px;

}

Page 32: May The Nodejs Be With You

@import

// bar.styl

.bar

width 10px

// style.styl

.foo

@import "bar.styl"

@media screen and (min-width: 640px)

@import 'bar.styl'

...

Try Stylus Online

// style.css

.foo .bar {

width: 10px;

}

@media screen and (min-width: 640px) {

.bar {

width: 10px;

}

}

Page 33: May The Nodejs Be With You

// package.json

{

...

"scripts": {

"stylus": "stylus -w --sourcemap-inline assets/css/main.styl -o public/css/main.css",

...

},

...

}

Page 34: May The Nodejs Be With You

Npm-run-all

A CLI tool to run multiple npm-scripts in parallel or sequential.

https://github.com/mysticatea/npm-run-all/

$ npm i -S npm-run-all

Page 35: May The Nodejs Be With You

// package.json

{

...

"scripts": {

"stylus": "stylus -w --sourcemap-inline assets/css/main.styl -o public/css/main.css",

"server": "nodemon app.js",

"dev": "npm-run-all --parallel stylus server"

},

...

}

$ npm run dev

Page 36: May The Nodejs Be With You

Autoprefixer

Parse CSS and add vendor prefixes to rules by Can I Use

https://github.com/postcss/autoprefixer/

Autoprefixer for stylus

https://github.com/jescalan/autoprefixer-stylus/

$ sudo npm i -g autoprefixer-stylus

Page 37: May The Nodejs Be With You

// package.json

{

...

"scripts": {

"stylus": "stylus -u autoprefixer-stylus \

-w --sourcemap-inline assets/css/main.styl -o public/css/main.css",

"server": "nodemon app.js",

"dev": "npm-run-all --parallel stylus server"

},

...

}

$ npm run dev

Page 38: May The Nodejs Be With You

// assets/stylus/main.styl

.foo

display flex

// public/css/style.css

.foo {

display: -webkit-box;

display: -ms-flexbox;

display: flex;

}

/*#

sourceMappingURL=data:application/json;base64

,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL2Fz

c2V0cy9zdHlsdXMvbWFpbi5zdHlsIl0sIm5hbWVzIjpbX

SwibWFwcGluZ3MiOiJBQUFBO0VBQ0ksT0FBTSxLQUFOO0

VBQ0EsU0FBUSxLQUFSIiwiZmlsZSI6Im1haW4uY3NzIiw

ic291cmNlc0NvbnRlbnQiOlsiYm9keVxuICAgIGNvbG9y

IHJlZFxuICAgIGRpc3BsYXkgZmxleCJdfQ== */

Page 39: May The Nodejs Be With You

BrowserSync

Synchronised browser testing

https://www.browsersync.io/

$ sudo npm i -g browser-sync

Page 40: May The Nodejs Be With You

// package.json

{

...

"scripts": {

"stylus": "stylus -u autoprefixer-stylus -w --sourcemap-inline assets/stylus/main.styl

-o public/css/main.css && browser-sync reload --port 4000 --files='public/css/main.css'",

"sync": "browser-sync start --ws -f '**/*' --proxy localhost:3000 --no-open --port 4000

--reload-delay=300 --reload-debounce=500",

"server": "nodemon -e js app.js",

"dev": "npm-run-all --parallel sync stylus server",

"start": "NODE_ENV=production node app.js",

"lint": "eslint --env browser, node app.js",

"test": "echo \"Error: no test specified\" && exit 1"

},

...

}

$ npm run dev

Page 41: May The Nodejs Be With You

May The Node.js Be With You

https://github.com/daliborgogic/may-the-nodejs-be-with-you/

SSH:

$ git clone [email protected]:daliborgogic/may-the-nodejs-be-with-you.git foo

HTTPS:

$ git clone https://github.com/daliborgogic/may-the-nodejs-be-with-you.git

Download ZIP

Page 42: May The Nodejs Be With You

Thank You

Ad :]

Vue.js

Intuitive, Fast and Composable MVVM for building interactive interfaces..

https://vuejs.org/

https://vuejs.org/v2/guide/comparison.html