Archive for the ‘nodejs’ Category

nodejs: npm

July 31, 2017

The post discusses npm.

environment

bash-3.2$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1510

what is npm
Package manager of nodejs

how to install it
It is installed automatically after nodejs is installed.

bash-3.2$ brew install node
bash-3.2$ node -v
v8.1.4
bash-3.2$ npm -v
5.0.3

conclusion
The post notes npm.

Advertisements

nodejs: mongoose module and mongodb

July 27, 2017

The post shows how a nodejs program connects to mongoDB with mongoose module.

version

bash-3.2$ node -v
v8.1.4

init nodejs project

bash-3.2$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (myapp) 
version: (1.0.0) 
description: 
entry point: (index.js) app.js
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/chengyihe/workspace/node/myapp/package.json:

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes) 

install mongoose module
By default, npm install modules locally rather than globally. The files related to mongoose module is put under $(pwd)/node_modules directory.

bash-3.2$ npm install mongoose
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN myapp@1.0.0 No description
npm WARN myapp@1.0.0 No repository field.

+ mongoose@4.11.4
added 31 packages in 6.164s
bash-3.2$ ls -l node_modules/
total 0
drwxr-xr-x   82 chengyihe  staff   2788 Jul 27 07:20 async
drwxr-xr-x    7 chengyihe  staff    238 Jul 27 07:20 bluebird
drwxr-xr-x   10 chengyihe  staff    340 Jul 27 07:20 bson
drwxr-xr-x    6 chengyihe  staff    204 Jul 27 07:20 buffer-shims
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 core-util-is
drwxr-xr-x   15 chengyihe  staff    510 Jul 27 07:20 debug
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 es6-promise
drwxr-xr-x    9 chengyihe  staff    306 Jul 27 07:20 hooks-fixed
drwxr-xr-x    7 chengyihe  staff    238 Jul 27 07:20 inherits
drwxr-xr-x   10 chengyihe  staff    340 Jul 27 07:20 isarray
drwxr-xr-x   12 chengyihe  staff    408 Jul 27 07:20 kareem
drwxr-xr-x  640 chengyihe  staff  21760 Jul 27 07:20 lodash
drwxr-xr-x   17 chengyihe  staff    578 Jul 27 07:20 mongodb
drwxr-xr-x   23 chengyihe  staff    782 Jul 27 07:20 mongodb-core
drwxr-xr-x   18 chengyihe  staff    612 Jul 27 07:20 mongoose
drwxr-xr-x   16 chengyihe  staff    544 Jul 27 07:20 mpath
drwxr-xr-x   10 chengyihe  staff    340 Jul 27 07:20 mpromise
drwxr-xr-x   12 chengyihe  staff    408 Jul 27 07:20 mquery
drwxr-xr-x    6 chengyihe  staff    204 Jul 27 07:20 ms
drwxr-xr-x   12 chengyihe  staff    408 Jul 27 07:20 muri
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 process-nextick-args
drwxr-xr-x   19 chengyihe  staff    646 Jul 27 07:20 readable-stream
drwxr-xr-x   11 chengyihe  staff    374 Jul 27 07:20 regexp-clone
drwxr-xr-x   10 chengyihe  staff    340 Jul 27 07:20 require_optional
drwxr-xr-x    6 chengyihe  staff    204 Jul 27 07:20 resolve-from
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 safe-buffer
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 semver
drwxr-xr-x    7 chengyihe  staff    238 Jul 27 07:20 sliced
drwxr-xr-x    7 chengyihe  staff    238 Jul 27 07:20 string_decoder
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 util-deprecate

mongodb client source code
The nodejs example loads mongoose module, connects to a mongoDB server, and then inserts a data satisfied a schema.

  • require(‘mongoose’): it loads mongoose module
  • mongoose.connect(): it connects to a database in a mongoDB server
  • Schema: specify the data structure of document in collection
  • Model: it corresponds to a collection in the connected mongoDB server. The first parameter specifies the collection name, the second one is a schema which specifies the format of documents within the collection
bash-3.2$ cat app.js 
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useMongoClient: true,})

var Schema = mongoose.Schema
var memberSchema = new Schema(
    {
        name: String
    }
)
var member = mongoose.model('member', memberSchema)
var person = new member({ name: 'chengyihe' })

person.save(function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log('saved');
    }
});

output of running the mongodb client
Output shows data is saved in mongoDB server successfully.

bash-3.2$ node app.js
(node:54460) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
saved

use mongodb shell to check the result
It shows that the test database’s members collection has a document which is inserted by above example.

bash-3.2$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Server has startup warnings:
2017-07-26T20:43:13.356+0800 I CONTROL  [initandlisten]
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten]
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten]
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> db.kernels.drop()
true
> show collections
members
> db.members.find()
{ "_id" : ObjectId("597936a8cff68dd4bc0b4c71"), "name" : "chengyihe", "__v" : 0 }
> 

conclusion
The post uses a nodejs with mongoose module to insert data in a mongoDB server. Then, it query the data with mongoDB shell.

nodejs: url module and query

July 24, 2017

The post discusses url module and query.

version

bash-3.2$ node -v
v8.1.4

sample source code

  • url.parse() transforms a url string into a url object
  • url.href transforms a url object into a url string
  • url.parse(urlString).query returns the query string of the url
  • url.parse(urlString, true).query returns the query object of the url
const querystring = require('querystring')
const url = require('url')

const urlString = 'https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash'
console.log(urlString)

const urlObj = url.parse(urlString)
console.log(urlObj)

const urlString2 = urlObj.href
console.log(urlString2)

const urlObj2 = url.parse(urlString2)
console.log(urlObj2)

console.log(url.parse(urlString).query)

console.log(url.parse(urlString, true).query)

sample output

bash-3.2$ node app.js 
https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash
Url {
  protocol: 'https:',
  slashes: true,
  auth: 'user:pass',
  host: 'sub.host.com:8080',
  port: '8080',
  hostname: 'sub.host.com',
  hash: '#hash',
  search: '?query=string',
  query: 'query=string',
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?query=string',
  href: 'https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash' }
https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash
Url {
  protocol: 'https:',
  slashes: true,
  auth: 'user:pass',
  host: 'sub.host.com:8080',
  port: '8080',
  hostname: 'sub.host.com',
  hash: '#hash',
  search: '?query=string',
  query: 'query=string',
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?query=string',
  href: 'https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash' }
query=string
{ query: 'string' }

conclusion
The post demonstrates a sample code to understand url object.

nodejs: querystring and json

July 24, 2017

The post discusses querystring and json.

software version

bash-3.2$ node -v
v8.1.4

sample source code
Both query and JSON could be objects and strings.

  • qeurystring.parse() could transform a query string into a query object
  • querystring.stringify() could transform a query object into a query string
  • JSON.parse() could transform a json string into a json object
  • JSON.stringify() could transform a json object into a json string
  • name=chengyihe&id=32 is a query string
  • {“name”:”chengyihe”,”id”:”32″} is a json string
const querystring = require('querystring')

var queryObj = {name: 'chengyihe', id: '32'}
console.log(queryObj)

var queryString = querystring.stringify(queryObj)
console.log(queryString)

var queryObj2 = querystring.parse(queryString)
console.log(queryObj2)

var jsonObj = {'name': 'chengyihe', 'id': '32'}
console.log(jsonObj)

var jsonString = JSON.stringify(jsonObj)
console.log(jsonString)

var jsonObj2 = JSON.parse(jsonString)
console.log(jsonObj2)

sample output

bash-3.2$ node app.js 
{ name: 'chengyihe', id: '32' }
name=chengyihe&id=32
{ name: 'chengyihe', id: '32' }
{ name: 'chengyihe', id: '32' }
{"name":"chengyihe","id":"32"}
{ name: 'chengyihe', id: '32' }

conclusion
The post elaborate the difference between query and json.

nodejs: parse http get request data

July 24, 2017

The post discusses how to parse http get request data.

software version

bash-3.2$ node -v
v8.1.4

server source code

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    var data = url.parse(req.url, true).query;
    console.log('req.url: ' + req.url);
    console.log('name: ' + data.name + ', id = ' + data.id);
    res.write('name: ' + data.name + ', id = ' + data.id);
    res.end('\n');
}).listen(3000);

output of running server

bash-3.2$ node app.js
req.url: /?name=chengyihe&id=32
name: chengyihe, id = 32

output of running client
By default, curl sends a http get request to a http server.

bash-3.2$ curl -i "http://127.0.0.1:3000/?name=chengyihe&id=32"
HTTP/1.1 200 OK
Content-Type: text/html
Date: Mon, 24 Jul 2017 14:21:53 GMT
Connection: keep-alive
Transfer-Encoding: chunked

name: chengyihe, id = 32

conclusion
The http.IncomingMessage.url has the http get data. This sample use require(‘url’).parse() to parse the data.

nodejs: parse http post data

July 24, 2017

This post discusses how to parse http post data in nodejs.

software version
$ node -v
v8.1.4

server source code

var http = require('http');
const querystring = require('querystring');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    if (req.method == 'POST') {
        var body = '';
        req.on('data', function(data) {
            body += data;
        });
        req.on('end', function(data) {
            console.log('body: ' + body);
            var parsedData = querystring.parse(body);
            res.write('name: ' + parsedData.name + '\n');
            res.write('id: ' + parsedData.id + '\n');
            res.end('process post finish\n');
        });
    }
}).listen(3000);

output of running client
curl sends post data with -d flag.

$ curl -i -d 'name=chengyihe&id=32' 127.0.0.1:3000
HTTP/1.1 200 OK
Content-Type: text/html
Date: Mon, 24 Jul 2017 14:05:56 GMT
Connection: keep-alive
Transfer-Encoding: chunked

name: chengyihe
id: 32
process post finish

output of running server

$ node app.js
body: name=chengyihe&id=32

conclusion
If http.IncomingMessage.method == ‘POST’, then this example use qeuerystring.parse() to parse post data on ‘end’ event.

nodejs: how to load json file

July 21, 2017

This post discusses how to load json files.

software version

bash-3.2$ node --version
v8.1.4

source code
The require function could help load json file as well as module. By default, the json file is put in node_modules directory.

const { name, id } = require('data');
console.log('name = ' + name)
console.log('id = ' + id);

json file

{
    "name": "chengyihe",
    "id": "32"
}

output

bash-3.2$ node app.js 
name = chengyihe
id = 32

conclusion
This post demonstrate how to load json files. It includes nodejs source code and the running output.

nodejs: how to create and load customised module

July 21, 2017

This post demonstrate an example to create and use customised module.

software version

bash-3.2$ node --version
v8.1.4

create an application
Create a simple application which is also a module.

console.log(module)

This application dumps its own module data within which module.paths indicates where it could load modules. It appears that modules are loaded from node_modules directory. The position of node_modules directory is searched from current directory, then form parent directory, then from parent’s parent directory, and so on.

$ pwd
/Users/chengyihe/workspace/node/myapp
bash-3.2$ node app.js
Module {
  id: '.',
  exports: {},
  parent: null,
  filename: '/Users/chengyihe/workspace/node/myapp/app.js',
  loaded: false,
  children: [],
  paths:
   [ '/Users/chengyihe/workspace/node/myapp/node_modules',
     '/Users/chengyihe/workspace/node/node_modules',
     '/Users/chengyihe/workspace/node_modules',
     '/Users/chengyihe/node_modules',
     '/Users/node_modules',
     '/node_modules' ] }

create node_modules directory to include module

bash-3.2$ mkdir node_modules

add hello module in node_modules directory

exports.name = 'chengyihe'
exports.city = 'taipei'

modify application to load hello module
Let the application load hello module, dump application module, and dump what hello module exports.

const hello = require('hello')
console.log(module)
console.log(hello);

output
The output of the application shows that it has a child module loaded from hello.js. What hello module exports is at line #24.

bash-3.2$ node app.js 
Module {
  id: '.',
  exports: {},
  parent: null,
  filename: '/Users/chengyihe/workspace/node/myapp/app.js',
  loaded: false,
  children: 
   [ Module {
       id: '/Users/chengyihe/workspace/node/myapp/node_modules/hello.js',
       exports: [Object],
       parent: [Circular],
       filename: '/Users/chengyihe/workspace/node/myapp/node_modules/hello.js',
       loaded: true,
       children: [],
       paths: [Array] } ],
  paths: 
   [ '/Users/chengyihe/workspace/node/myapp/node_modules',
     '/Users/chengyihe/workspace/node/node_modules',
     '/Users/chengyihe/workspace/node_modules',
     '/Users/chengyihe/node_modules',
     '/Users/node_modules',
     '/node_modules' ] }
{ name: 'chengyihe', city: 'taipei' }

conclusion
An application is a module. By default, it could load module from node_modules directory. The syntax to load module is require(‘module_name’).

nodejs: expressjs: use express generator to create web applications

July 20, 2017

This post discusses using express generator to create web applications.

software version

bash-3.2$ node -v
v8.1.4
bash-3.2$ npm -v
5.0.3
bash-3.2$ express --version
4.15.0

what is express generator
A command line tool to automatically generate web application skeleton.

install npm generator

bash-3.2$ npm install express-generator -g
/usr/local/bin/express -&gt; /usr/local/lib/node_modules/express-generator/bin/express-cli.js
+ express-generator@4.15.0
added 7 packages in 1.979s
bash-3.2$ express -h

  Usage: express [options] [dir]

  Options:

    -h, --help           output usage information
        --version        output the version number
    -e, --ejs            add ejs engine support
        --pug            add pug engine support
        --hbs            add handlebars engine support
    -H, --hogan          add hogan.js engine support
    -v, --view   add view  support (dust|ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
    -c, --css    add stylesheet  support (less|stylus|compass|sass) (defaults to plain css)
        --git            add .gitignore
    -f, --force          force on non-empty directory

use express generator to create web applications

bash-3.2$ express --view=pug myapp

   create : myapp
   create : myapp/package.json
   create : myapp/app.js
   create : myapp/public
   create : myapp/routes
   create : myapp/routes/index.js
   create : myapp/routes/users.js
   create : myapp/views
   create : myapp/views/index.pug
   create : myapp/views/layout.pug
   create : myapp/views/error.pug
   create : myapp/bin
   create : myapp/bin/www
   create : myapp/public/javascripts
   create : myapp/public/images
   create : myapp/public/stylesheets
   create : myapp/public/stylesheets/style.css

   install dependencies:
     $ cd myapp && npm install

   run the app:
     $ DEBUG=myapp:* npm start

run the app: a web server

bash-3.2$ cd myapp && npm install
bash-3.2$ DEBUG=myapp:* npm start

> myapp@0.0.0 start /Users/chengyihe/workspace/node/myapp
> node ./bin/www

  myapp:server Listening on port 3000 +0ms

use browser to see the web page

what does npm start do
By default, it calls scripts.start property. In this case, scripts.start = “node ./bin/www”. Therefore, executing “npm start” is equivalent to executing “node ./bin/www”.

{
  "name": "myapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.17.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.3",
    "express": "~4.15.2",
    "morgan": "~1.8.1",
    "pug": "~2.0.0-beta11",
    "serve-favicon": "~2.4.2"
  }
}

conclusion
The post shows how to use express generator tool to create a simple web server.

nodejs: expressjs: a simple web server

July 20, 2017

This post discusses how to create a simple server by express.js.

software version

$ node -v
v8.1.4
$ npm -v
5.0.3

what is express.js
A web frameworks based on nodejs

use npm to create an application dependent on express module
Use npm init to create an application, myapp. Set app.js as entry point of myapp. Then call npm install express to make express module a dependency of myapp.

$ mkdir myapp
$ cd myapp
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (myapp)
version: (1.0.0)
description:
entry point: (index.js) app.js
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/chengyihe/workspace/node/myapp/package.json:

{
"name": "myapp",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

Is this ok? (yes)
$ npm install express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN myapp@1.0.0 No description
npm WARN myapp@1.0.0 No repository field.

+ express@4.15.3

create a web server with express module
Below is a server to handle get, post and put request to / path.

const express = require('express')
const app = express()

app.get('/', function (req, res) {
res.send('Response for a GET request\n')
})

app.post('/', function (req, res) {
res.send('Response for a POST request\n')
})

app.put('/', function (req, res) {
res.send('Response for a PUT request\n')
})

app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})

Run the server

$ node app.js
Example app listening on port 3000!

client gets different response while using different htp request

$ curl -i -X GET http://127.0.0.1:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 27
ETag: W/"1b-pN9sXU7r0A2IQd0ldy0JSeuB4Kc"
Date: Thu, 20 Jul 2017 01:56:39 GMT
Connection: keep-alive

Response for a GET request
$ curl -i -X POST http://127.0.0.1:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 28
ETag: W/"1c-ElHCAfDja6bDIja0jD2CpSBPkUg"
Date: Thu, 20 Jul 2017 02:02:59 GMT
Connection: keep-alive

Response for a POST request
$ curl -i -X PUT http://127.0.0.1:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 27
ETag: W/"1b-urf98BOc+00dqwrJXZgwCKqWe8w"
Date: Thu, 20 Jul 2017 02:03:27 GMT
Connection: keep-alive

Response for a PUT request

conclusion
The post show a simple web server created by express.js.


%d bloggers like this: