NodeJS学习笔记

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 MongoDB,通用型 2核4GB
简介: nodejs npm安装代理设置: npm config set proxy http://username:password@host:port 安装第三方模块 npm install mongodb/mysql/express/redis/.
nodejs npm安装代理设置:

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;
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
8月前
|
前端开发
前端学习笔记202306学习笔记第四十九天-学习node的必要性1
前端学习笔记202306学习笔记第四十九天-学习node的必要性1
33 0
|
8月前
|
JavaScript 前端开发
前端学习笔记202305学习笔记第二十四天-node.js安装
前端学习笔记202305学习笔记第二十四天-node.js安装
34 0
|
8月前
|
前端开发
前端学习笔记202306学习笔记第四十三天-学习node的必要性1
前端学习笔记202306学习笔记第四十三天-学习node的必要性1
37 0
|
8月前
|
前端开发
前端学习笔记202306学习笔记第四十三天-学习node的必要性2
前端学习笔记202306学习笔记第四十三天-学习node的必要性2
46 0
|
8月前
|
前端开发
前端学习笔记202306学习笔记第四十三天-安装多个版本的node之2
前端学习笔记202306学习笔记第四十三天-安装多个版本的node之2
52 0
前端学习笔记202306学习笔记第四十三天-安装多个版本的node之2
|
8月前
|
前端开发 JavaScript
前端学习笔记202306学习笔记第四十三天-在node版本中运行js之1
前端学习笔记202306学习笔记第四十三天-在node版本中运行js之1
40 0
|
8月前
|
前端开发
前端学习笔记202306学习笔记第四十三天-安装多个版本的node之1
前端学习笔记202306学习笔记第四十三天-安装多个版本的node之1
46 0
|
8月前
|
前端开发 JavaScript
前端学习笔记202306学习笔记第四十三天-在node版本中运行js之2
前端学习笔记202306学习笔记第四十三天-在node版本中运行js之2
48 0
|
8月前
|
前端开发
前端学习笔记202306学习笔记第四十三天-安装node之1
前端学习笔记202306学习笔记第四十三天-安装node之1
40 0
|
8月前
|
前端开发
前端学习笔记202306学习笔记第四十三天-node提供的全局变量1
前端学习笔记202306学习笔记第四十三天-node提供的全局变量1
38 0