nodejs+express+mongoDB知识点记录
分页查询(limit、skip、sort)limit()指定查询结果数量db.COLLECTION_NAME.find().limit(NUMBER)skip()指定查询偏移量db.COLLECTION_NAME.find().limit(...
...

sokect.io的使用

服务端

const socketio = require ('socket.io');

socket = server => {
const io = socketio.listen (server);
io.on ('connection', function (socket) {
// to do somethings
// let token = socket.handshake.query.token;
console.log ('connection socket');

// // 使用计时器向客户端发送数据
setInterval (() => {
socket.emit ('msg', new Date ().getTime ());
}, 1000 * 60);
});
};

module.exports = socket;

客户端


import io from 'socket.io-client';
import config from '../config';

const socket = io (config.socketUrl, {
// 是否自动重新连接
reconnection: true,
// 自动重连10次后放弃
reconnectionAttempts: 15,
// 自动重连间隔时间
reconnectionDelay: 3000,
// 发送参数给服务器,服务端获取参数 socket.handshake.query
query: {
accessToken: '1212312',
},
});

socket.on ('connect', () => {
console.log ('connected!');
});

socket.on ('msg', (...msgs) => {
console.warn (msgs);
});