标签归档:Node.js

使用n管理Node.js版本

Use n to manage your Node.js versions

Installation

  1. Install n through npm:(Recommended)
npm install -g n

Configurations

Since n‘s default path is /usr/local/n, it will need super user’s permission to modify file systems in the default path, we need to config n‘s path to user’s path.

config N_PREFIX

  1. edit bash profile
    vim ~/.bashrc
    
  2. add line
    export N_PREFIX=~/.n
    
  3. apply file
    source ~/.bashrc
    

config PATH

  1. edit bash profile
    vim ~/.bashrc
    
  2. add a line
    export PATH=$HOME/.n/bin:$PATH
    
  3. apply file
    source ~/.bashrc
    

Use

# Use or install a version of node
n 9.0.0
# Use or install the latest official release
n latest
# Use or install the stable official release
n stable
# Use or install the latest LTS official release
n lts

Type n to show list of versions.
And select a version by up down button.

$ n
    node/8.9.3
    node/9.0.0
    node/9.2.1
  ο node/9.3.0

node.js – Difference between app.use and app.get in express.js

http://stackoverflow.com/questions/15601703/difference-between-app-use-and-app-get-in-express-js

app.use() is intended for binding middleware to your application. The path is a “mount” or “prefix” path and limits the middleware to only apply to any paths requested that begin with it. It can even be used to embed another application:

// subapp.js
var express = require('express');
var app = modules.exports = express();
// ...
// server.js
var express = require('express');
var app = express();
app.use('/subapp', require('./subapp'));
// ...

By specifying / as a “mount” path, app.use() will respond to any path that starts with /, which are all of them and regardless of HTTP verb used:
GET /
PUT /foo
POST /foo/bar
etc.
app.get(), on the other hand, is part of Express’ application routing and is intended for matching and handling a specific route when requested with the GET HTTP verb:
GET /
And, the equivalent routing for your example of app.use() would actually be:

app.all(/^/.*/, function (req, res) {
    res.send('Hello');
});

Node.js 连接 mongodb 教程

新建一个文件夹存放我们的js文件
1.package.json
使用mongodb

{
  "name": "test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
  "start": "node app.js"
  },
  "dependencies": {
  "mongodb":"1.4.7"
  }
}

2.创建文件mongo.js

var MongoClient = require('mongodb').MongoClient;
var db;
var connected = false;
module.exports = {
  connect: function(url, callback){
  MongoClient.connect(url, function(err, _db){
    if (err) { throw new Error('Could not connect: '+err); }
    db = _db;
    connected = true;
    callback(db);
    });
  },
  collection: function(name){
    if (!connected) {
    throw new Error('Must connect to Mongo before calling "collection"');
    }
    return db.collection(name);
  }
};

3.创建app.js

var mongo = require('./mongo');
var mongoUrl = "mongodb://localhost:27017/test";
mongo.connect(mongoUrl, function(){
console.log('Connected to mongo at: ' + mongoUrl);
var coll = mongo.collection('users');
var userObject = {
  username: "admin",
  password: "admin"
};
// create the new user
coll.insert(userObject, function(err,user){
  console.log("created user");
});
coll.find().toArray(function(err, results) {
  console.dir(results);
  });
});

 
源码下载:https://github.com/shengoo/mongotest
效果如下:
mongodb test
 

Node.js教程起步资料

Tutorials

Videos

Screencasts

Books

Courses

Blogs

Podcasts

JavaScript resources

Node Modules

Other