-
Create server connection:
a. Simple connecting: var app = require('vn.io').listen(3000);
b. Connect with http or https server: var app = require('vn.io'); var http = require('http');
http.createServer(app).listen(3000);
c. Using middlewares: var app = require('vn.io').listen(3000);
app.use(cookieParser()); app.use(bodyParser());
d. Using socket: var app = require('vn.io'); var http = require('http'); var Socket = require('vn.io/ws');
Socket.init(http.createServer(app).listen(3000));
-
Using web mvc:
a. Create Server: - See in <1.a|b|c>
b. Using view template:
- in index.js: var mvc = require('vn.io/mw/mvc'); app.use(mvc('./mvc/')); app.use(require('vn.io/mw/view')); - in /mvc/controller/account.js: module.exports = function AccountController(get, post){ this.get_login = function(fn){ fn({ name: 'Thuan' }); }; }; - in /mvc/view/account/header.html: <h1>This is header</h1> - in /mvc/view/account/login.html: {{ use('header') }} {{ var time = require('time'); }} {{ time() }} Hello World, I am {{ name }}. {{ use('footer') }}
c. Using api:
- in index.js: var mvc = require('vn.io/mw/mvc'); app.use(mvc('./view/mvc/')); app.use(require('vn.io/mw/view')); - in /mvc/controller/account.js: module.exports = function AccountController(get, post){ this.post_login = function(fn){ console.log(post); fn(false, {data}); // => {"error": false, message: {data}} }; };
-
Using WebSocket:
a. Create server: - See in <1.d>;
b. Create folder of socket: - /path/to/project/socket
c. Create a server example: - Server: [ in file: /path/to/project/socket/chat.js ]
var fs = require('fs'); var Socket = require('vn.io/ws'); var io = Socket.of('chat'); io.connect(function(socket){ socket.use(function(next){ // check origin host pass to server if(!socket.match_origin(['host1', 'host2'])){ return next(0); } }); // set user.id pass to socket.id socket.on('init', function(data){ io.set(data.id, socket); socket.use(); }); // join to room chat socket.on('join', function(roon){ socket.join(room); }); // leave from from room chat socket.on('leave', function(room){ socket.leave(room); }); // chatting to the room socket.on('chat', function(data){ // data: {room, user, text} // send for all client of the room, using: io.in(room) or socket.in(room) io.in(data.room).emit('chat', data); socket.in(data.room).emit('chat', data); // send to all client without this client, using: socket.to(room) socket.to(data.room).emit('chat', data); }); // get all list chat of the room socket.on('list-chat', function(query, fn){ // query: {room, time} // fn: this is a callback function to response to the client // select * from Chat where room = 'query.room' and time < 'query.time' limit 10 Chat.find({room: query.room, time: {$lt: query.time}}, {$limit: 10}, fn); }); // get a file from client side socket.on('send-file', function(info, chunk){ // if has chunk => write it var stream = fs.createWriteStream(path, {flags: 'a', defaultEncoding: 'binary'}); stream.write(chunk); }); // send a file to client socket.on('send-to-client', function(data, fn){ var stream = fs.createReadStream('./image.png'); stream.on('data', fn); stream.on('end', fn); }); // upload file socket.on('upload', function(data, fn){ // data: {name, binary} var stream = fs.createWriteStream(path, {flags: 'a', defaultEncoding: 'binary'}); stream.write(data.binary); stream.on('drain', fn); }); socket.on('read-file', function(info, fn){ // info: {id, type} // => See socket.on('send-to-client'); }); }); module.exports = io;
d. Create Client Side example: - Server: [ in /path/to/project/folder/file.js ]
var fs = require('fs'); var WebSocket = require('vn.io/ws/client'); var socket = new WebSocket('http://localhost:3000/chat'); // send data to init server socket.emit('init', {id: cookie.get('id')}); // send a file to server var stream = fs.createReadStream(path); stream.on('data', function(chunk){ socket.emit('send-file', {name: 'file-name.ext'}, chunk); }); stream.on('end', function(){ console.log('done'); }); // get a file from server socket.on('send-to-client', function(chunk){ // write file with chunk var stream = fs.createWriteStream('./upload/a.jpg', {flags: 'a', defaultEncoding: 'binary'}); stream.write(data); });
e. Create Client Browser: - Client: [ in /public/asset/js/client.js ]
var Socket = require('vn.io/lib/socket'); var socket = new Socket('http://localhost:3000/chat'); socket.emit('init', {id: cookie.get('id')}); // upload file $('#input').onchange = function(e){ var reader = new FileReader(), file = e.target.files[0]; // load binary data and send it reader.onload = function(e){ socket.emit('upload', { name: file.name, binary: e.target.result }, function(data_success){ console.log(data_success); }); }; // if need progress reader.onprogress = function(e){ console.log('loaded: ', e.loaded); console.log('total: ', e.total); }; reader.readAsBinaryString(file); }; socket.on('read-file', function(binary){ console.log(binary); });
-
Using MongoDB:
a. Create model: [ in /mvc/model/User.js: [ collection: 'User' ] ]
var Mongo = require('vn.io/db/mongo'); module.exports = class User extends Mongo {};
b. using:
var User = require('/mvc/model/User'); // find many document var user = new User(); user.find({name: 'Thuan'}, {field: 1}, {limit: 10}, function(rows){}).catch(function(e){}); // or using static method User.find({name: 'Thuan'});
c. method: .find .findOne .update .insert .save .remove .aggregate
.findAndModify .findAndRemove({_id: 1}); .findAndUpdate({_id: 1}, {update_query}); .findAndInsert({_id: 1}, {new_document});
-
Using MariaDB:
a. Create Model: [ in /mvc/model/User.js: [ table: 'User' ] ]
var Maria = require('vn.io/db/sql'); module.exports = class User extends Maria {};
b. Using:
var User = require('/mvc/model/User'); var user = new User(); user.where({'User.username': 'nguyenthuan'}); user.column({ User: ['column1, column2, ...'], UserLogin: ['column1, column2, column...'] }); user.limit(10); user.leftJoin({ UserLogin: 'User.id = UserLogin.id' }); user.fetchAll(fn); => SELECT User.column1, User.column2, UserLogin.column1, UserLogin.column2, ... FROM User LEFT JOIN UserLogin ON User.id = UserLogin.id WHERE User.username = 'nguyenthuan'
c. Method: .where: where({a: 1, b: 2}) -> where a = 1 and b = 2 where('a = 1 and b = 2')
.column: column('a, b, c') -> select a, b, c column({ -> select A.a, A.b, B.c, B.d A: ['a', 'b'], B: ['c', 'd'] }) .limit: limit(10) limit(0, 10) .leftJoin leftJoin({ B: 'A.a = B.b' }) -> left join B on A.a = B.b .rightJoin .join .fetch .fetchAll .insert .update where({id: 10}).update({name: 'New Name'}); .remove
d. Create table: - in /path/to/file.js:
var Sql = require('vn.io/db/sql'); var define = require('vn.io/define'); var db = new Sql(); // set model dir define.set({ model: '/mvc/model/' }); db.create('User', function(table){ table.increment('id'); => 'id bigint(20) autoincrement' table.varchar('username' 40); => 'username varchar(40)' table.index(['username'], {type: 'unique'}); table.index(['fname', 'lname'], {type: 'fulltext', name: 'name'}); // don't forget return it return table; }); - open terminal and run command: node /path/to/file.js
-
Using Redis:
var redis = require('vn.io/db/redis');
redis('hget', 'email', 'haohannghetinh@gmail.com').then(function(id){ console.log(id); });
-
Using config.json:
a. Location: /path/to/project/config.json
{ "maria": { "host": "localhost", "port": 3306, "user": "thuychi", "password": "thuan1992", "db": "test", "multiStatements": true, "charset": "UTF8" }, "redis": { "host": "127.0.0.1", "port": 6379, "auth": "6eeb73df91baf0106f3322fb4a5440cda1b04b88", "option": { "detect_buffers": true }, "table": ["default", "user", "friend", "chat"] }, "mongodb": "mongodb://localhost:27017/demo" }
vn.io
1.0.3 • Public • PublishedReadme
Keywords
nonePackage Sidebar
Install
npm i vn.io
Weekly Downloads
14
Version
1.0.3
License
ISC