nodejs npm安装代理设置:
npm install mongodb/mysql/express/redis/...
nodejs重点学习内容:
nodejs + express : Web MVC
nodejs + redis : 操作Key/Value
nodejs + mongodb : 操作文档型数据库
nodejs + mysql : 操作关系型数据库
nodejs + rabbitMQ :
nodejs socket : 网络编程
nodejs http :
nodejs fs : 文件系统
nodejs 开发环境的使用:WebStorm 8.0.4
nodejs模块开发概念 : module/exports/require
官方API : http://nodejs.cn/api/
//===========================================
mysql实例 :
var mysql = require('mysql');
var config = {'host':'127.0.0.1','port':3306,'user':'your username','password':'your pwd','database':'your dbname'};
var client = mysql.createConnection(config);
client.connect(function(err){
if(err){
console.log('disconnect');
} else {
console.log('connect');
}
});
client.query('select * from city',function(err,res,fields){
console.log(res);
client.end();
});
//===========================================
mongodb实例 :
/* mongodb demo */
var mongodb = require('mongodb');
var server = new mongodb.Server('127.0.0.1', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
console.log('connect db');
db.createCollection('mycoll', {safe: true}, function (err, collection) {
if (err) {
console.log(err);
} else {
var temp1 = {id: '1', title: 'hello', number: 1};
/*
collection.insert(temp1,{safe:true},function(err,result){
console.log(result);
});
*/
// console.log(collection);
collection.find().toArray(function (err, doc) {
// console.log('find');
console.log(doc);
});
}
});
} else {
console.log(err);
}
});
//===========================================
redis实例 :
/* redis demo */
var redis = require('redis');
var client = redis.createClient(6379,'127.0.0.1');
client.on("", function(err){
console.log(err);
return false;
});
// 如果设置了密码
client.auth('your password');
/*
client.set('key','value',function(err,reply){
if(err){
console.log(err);
return;
}
});
*/
client.get('key',function(err,reply){
if(err){
console.log(err);
return;
}
console.log(reply);
client.quit();
});
client.del('key');
//===========================================
http实例 :
/* http demo */
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Node.js</h1>');
res.end('<p>hello world</p>');
}).listen(5222);
console.log("http server is listening at port 5222");
//===========================================
socket实例 :
var net = require('net');
var server = net.createServer(function (c) {
console.log('server connected');
c.on('end', function () {
console.log('server disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(5222, function () {
console.log('server bound');
});
//===========================================
express实例 :
var express = require('express');
var app = express();
var config = {'host':'127.0.0.1','port':3306,'user':'your name','password':'your pwd','database':'your dbname'};
app.get('/test', function (req, res) {
res.writeHead(200, {'Content-Type': 'application/x-json;charset=UTF-8'});
res.write(config.toString());
res.end();
});
app.post('/add', function (req, res) {
});
app.listen(8080);
var log = require('./log');
log('web start.......');
//===========================================
module实例 :
var log = function(msg){
console.log(msg);
}
module.exports = log;
npm config set proxy http://username:password@host:port
安装第三方模块npm install mongodb/mysql/express/redis/...
nodejs重点学习内容:
nodejs + express : Web MVC
nodejs + redis : 操作Key/Value
nodejs + mongodb : 操作文档型数据库
nodejs + mysql : 操作关系型数据库
nodejs + rabbitMQ :
nodejs socket : 网络编程
nodejs http :
nodejs fs : 文件系统
nodejs 开发环境的使用:WebStorm 8.0.4
nodejs模块开发概念 : module/exports/require
官方API : http://nodejs.cn/api/
//===========================================
mysql实例 :
var mysql = require('mysql');
var config = {'host':'127.0.0.1','port':3306,'user':'your username','password':'your pwd','database':'your dbname'};
var client = mysql.createConnection(config);
client.connect(function(err){
if(err){
console.log('disconnect');
} else {
console.log('connect');
}
});
client.query('select * from city',function(err,res,fields){
console.log(res);
client.end();
});
//===========================================
mongodb实例 :
/* mongodb demo */
var mongodb = require('mongodb');
var server = new mongodb.Server('127.0.0.1', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
console.log('connect db');
db.createCollection('mycoll', {safe: true}, function (err, collection) {
if (err) {
console.log(err);
} else {
var temp1 = {id: '1', title: 'hello', number: 1};
/*
collection.insert(temp1,{safe:true},function(err,result){
console.log(result);
});
*/
// console.log(collection);
collection.find().toArray(function (err, doc) {
// console.log('find');
console.log(doc);
});
}
});
} else {
console.log(err);
}
});
//===========================================
redis实例 :
/* redis demo */
var redis = require('redis');
var client = redis.createClient(6379,'127.0.0.1');
client.on("", function(err){
console.log(err);
return false;
});
// 如果设置了密码
client.auth('your password');
/*
client.set('key','value',function(err,reply){
if(err){
console.log(err);
return;
}
});
*/
client.get('key',function(err,reply){
if(err){
console.log(err);
return;
}
console.log(reply);
client.quit();
});
client.del('key');
//===========================================
http实例 :
/* http demo */
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Node.js</h1>');
res.end('<p>hello world</p>');
}).listen(5222);
console.log("http server is listening at port 5222");
//===========================================
socket实例 :
var net = require('net');
var server = net.createServer(function (c) {
console.log('server connected');
c.on('end', function () {
console.log('server disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(5222, function () {
console.log('server bound');
});
//===========================================
express实例 :
var express = require('express');
var app = express();
var config = {'host':'127.0.0.1','port':3306,'user':'your name','password':'your pwd','database':'your dbname'};
app.get('/test', function (req, res) {
res.writeHead(200, {'Content-Type': 'application/x-json;charset=UTF-8'});
res.write(config.toString());
res.end();
});
app.post('/add', function (req, res) {
});
app.listen(8080);
var log = require('./log');
log('web start.......');
//===========================================
module实例 :
var log = function(msg){
console.log(msg);
}
module.exports = log;