Node.js
NPM
require

You should know

 

  • console.log()
    useful command function for debug.

     
  • Bower
    a front-end package manager tool.

Tools

What is Node.js

Server side JavaScript 

https://nodejs.org/

What is NPM

Node.js package manager

> npm install <package-name>
> npm install jquery

How NPM

example:

Why NPM

   require('package-name')

How to use package

the function for import package

var foo = require('package-name');
var $ = require('jquery');

// then you can use $ as jQuery function

How to use package

example: demo.js

Ooops!! jquery got some problem in Node.js

> npm install lodash
var _ = require('lodash');

var myArray = ['spring','summer','winter'];

_.each(myArray, function(s) {
    console.log(s);
});

Example

2. create demo.js file, and require package.

1. in cmd : npm package lodash

> node demo

3. in cmd : run demo.js

> npm install lodash
var _ = require('lodash');

var myArray = ['spring','summer','winter'];

_.each(myArray, function(s) {
    console.log(s);
});

Example

2. create demo.js file, and require package.

1. in cmd : npm package lodash

> node demo

3. in cmd : run demo.js

> npm install lodash
var _ = require('lodash');

var myArray = ['spring','summer','winter'];

_.each(myArray, function(s) {
    console.log(s);
});

Example

2. create demo.js file, and require package.

1. in cmd : npm package lodash

> node demo

3. in cmd : run demo.js

> npm install lodash
var _ = require('lodash');

var myArray = ['spring','summer','winter'];

_.each(myArray, function(s) {
    console.log(s);
});

Example

2. create demo.js file, and require package.

1. in cmd : npm package lodash

> node demo

3. in cmd : run demo.js

Demo - express

var express = require('express');

var app = express();

app.listen(3000);
> npm install express

app.js

> node app

Demo - express

var express = require('express');

var app = express();

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});

app.listen(3000);

app.js

Demo - express

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

var data = {
  '001':{'name':'Jayson','ext':'#7540'},
  '002':{'name':'Test','ext':'#9999'}
  };

app.get('/api', function (req, res) {
  res.json(data);
});

app.listen(3000);

app.js

Demo - express

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

var data = {
  '001':{'name':'Jayson','ext':'#7540'},
  '002':{'name':'Test','ext':'#9999'}
  };

app.get('/api/:id', function (req, res) {
  var id = req.params.id;
  res.json(data[id]);
});

app.listen(3000);

app.js

Demo - gulp

> npm install gulp-gm
> npm install --global gulp
> npm install gulp

Demo - gulp

var gulp = require('gulp');
var gm = require('gulp-gm');

gulp.task('default', function() {

    gulp.src("src/**/*.{jpg,png,bmp}")
        .pipe(gm(function(gmfile) {
            return gmfile.resize(260, 260);
        }))
        .pipe(gulp.dest("dist/img"));
});

gulpfile.js

Demo - gulp

> gulp

Hosting -- Open Shift

https://www.openshift.com/

Node.js

> node filename.js
> node
> node filename
quit: [Ctrl + C * 2]  
command mode:
execute program:

Tools

Visual Studio 2015

Visual Studio Code

Sublime Text 2/3

free ~ $5999

free

free / $70

50MB

9GB

20MB

NPM

> npm init
> npm install  <PackageName>
> npm uninstall <PackageName>
install:
uninstall:
make package-file:

NuGet , NPM , Bower (1)

NuGet

NPM

Bower

.NET package

Node.js package

front-end package

> Install-Package <package-name>
> npm install <package-name>
> bower install <package-name>

bower.json

NuGet

NPM

Bower

packages.config

package.json

bower.json

/Scripts

/Styles

/... 

/node_modules

/bower_components

NuGet , NPM , Bower (2)

require()

var bar = require("./file-name.js");
var foo = require("package-name");
var bar = require("./file-name");
import files:
import package:
  • find local package first, then global package.
  • must have "./" for file.

to be continued...

( and install Node.js ASAP...)

Node.js

By Jayson Chiang

Node.js

NodeJS basic I - npm, require

  • 730