All Projects → wenkeShi → Wxapp

wenkeShi / Wxapp

🙋travelib-node 流动图书Node.js后端

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Wxapp

Mean Angular4 Chat App
MEAN stack with Angular 4 Chat App
Stars: ✭ 41 (-68.94%)
Mutual labels:  mongoose, express, websocket
Express Starter
🚚 A boilerplate for Node.js, Express, Mongoose, Heroku, Atlas, Nodemon, PM2, and Babel. REST / GraphQL API Server | PaaS | SaaS | CI/CD | Jest | Supertest | Docker | MongoDB | PostgreSQL | Sequelize | Lodash | RxJS | JWT | Passport | WebSocket | Redis | CircleCI | Apollo | DevSecOps | Microservices | Backend Starter Kit | ES6
Stars: ✭ 353 (+167.42%)
Mutual labels:  mongoose, express, pm2
Angular Full Stack
Angular Full Stack project built using Angular, Express, Mongoose and Node. Whole stack in TypeScript.
Stars: ✭ 1,261 (+855.3%)
Mutual labels:  mongoose, express
Boilerplate Vue Apollo Graphql Mongodb
Start your magical stack journey!
Stars: ✭ 85 (-35.61%)
Mutual labels:  mongoose, express
Ecommerce Nodejs
Ecommerce application back-end codes
Stars: ✭ 97 (-26.52%)
Mutual labels:  mongoose, express
Cheatsheets
JavaScript and Node.js cheatsheets
Stars: ✭ 1,191 (+802.27%)
Mutual labels:  mongoose, express
Node Express Mongoose
A boilerplate application for building web apps using node and mongodb
Stars: ✭ 1,221 (+825%)
Mutual labels:  mongoose, express
Nodefony Starter
Nodefony Starter Node.js Framework
Stars: ✭ 95 (-28.03%)
Mutual labels:  websocket, pm2
Angular Seed Express
[DEPRECATED, Please use https://github.com/vyakymenko/angular-express] Extensible, reliable and modular starter project for Angular 7 with statically typed build AoT compilation, Express server and PM2 Daemon.
Stars: ✭ 107 (-18.94%)
Mutual labels:  express, pm2
Questionnaire
📋 问卷系统
Stars: ✭ 128 (-3.03%)
Mutual labels:  mongoose, express
Light Push
轻量级推送服务和实时在线监控平台,同时用于开发即时通信系统,基于node的socket.io,支持web、android、ios客户端,支持移动端离线推送,可进行分布式部署
Stars: ✭ 128 (-3.03%)
Mutual labels:  websocket, pm2
Api
API Oficial do Te Emprego
Stars: ✭ 73 (-44.7%)
Mutual labels:  mongoose, express
Mean Stack Angular6 Crud Example
MEAN Stack Angular 6 CRUD Web Application
Stars: ✭ 69 (-47.73%)
Mutual labels:  mongoose, express
Fullstack Shopping Cart
MERN stack shopping cart, written in TypeScript
Stars: ✭ 82 (-37.88%)
Mutual labels:  mongoose, express
Http Proxy Middleware
⚡ The one-liner node.js http-proxy middleware for connect, express and browser-sync
Stars: ✭ 8,730 (+6513.64%)
Mutual labels:  express, websocket
Chat.io
A Real Time Chat Application built using Node.js, Express, Mongoose, Socket.io, Passport, & Redis.
Stars: ✭ 1,325 (+903.79%)
Mutual labels:  mongoose, express
Express Rest Boilerplate
⌛️ Express starter for building RESTful APIs
Stars: ✭ 1,794 (+1259.09%)
Mutual labels:  mongoose, express
Node Production
Take Your Node.js Project to The Production Environment (VPS/Dedicated Server).
Stars: ✭ 35 (-73.48%)
Mutual labels:  express, pm2
Es6 Express Mongoose Passport Rest Api
Lightweight boilerplate for Node RESTful API, ES6, Express, Mongoose and Passport 🎁
Stars: ✭ 36 (-72.73%)
Mutual labels:  mongoose, express
Nightcat
个人博客,技术栈:NodeJS + Express + mongoose + GraphQL + Vue 全家桶
Stars: ✭ 97 (-26.52%)
Mutual labels:  mongoose, express

流动图书馆

流动图书馆是一个图书漂流和借阅工具,旨在共享闲置图书,并链接趣味相投的小伙伴。

预览

链接

image

技术栈

  • 小程序MINA框架: 一个响应的数据绑定框架。分为两块视图层(View)和逻辑层(App Service)
  • Flex:flex弹性布局
  • Express : http服务框架
  • websocket: 前后端消息的实时推送
  • mongoose: 操作mongodb数据库
  • pm2: 服务端使用pm2部署,常驻进程

截图

首页

1.png

借阅书架

2.png

发布的图书

3.png

借阅的图书

4.png

客户端

  • 代码结构

image.png

微信小程序中每个页面会有四个文件 .js .json .wxml .wxss js文件中是页面的逻辑,json文件是页面的一些配置,wxml是小程序的页面结构,wxss为页面的样式。

  • 封装http请求
const request = (obj) => {
  if(obj.header){
    obj.header.sessionId = session.sessionId;
  }else{
    obj.header = { sessionId: session.sessionId};
  }
  wx.request(obj);
}

在请求头中手动加上sessionId,因为小程序没有cookie。

  • websocket
//连接websocket
wx.connectSocket({
  url: 'wss://liudongtushuguan.cn/socket?sessionId=' + session.sessionId,
});

wx.onSocketOpen(function(res){

});
wx.onSocketClose(function(res){
  console.log('websocket closed');
});

wx.onSocketMessage(function(res){  //收到消息的回调
  let msg = JSON.parse(res.data);
  let msgs = that.data.borrowMessage;
  msgs.unshift(msg);
  that.setData({ borrowMessage: msgs});
});

//发送socket消息
  let data = JSON.stringify({
    targetId: bookData.ownerId,
    nickName: APP.globalData.userInfo.nickName,
    bookName: bookData.title,
    time: new Date().toLocaleString(),
    bookId: bookId,
    wxNum: wxNum,
    phoneNum: phoneNum,
    msg: msg,
  });
  wx.sendSocketMessage({
    data: data,
  });

服务端

  • 代码目录

image.png

  • Express框架实现http服务
const https = require('https');
const fs = require('fs');
const express = require('express')
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const app = express();
const queryString = require('querystring');
const URL = require('url');
const socket = require('./service/socket');

const router = require('./routes/router').router;

//获取认证证书
var key = fs.readFileSync('./key/2_www.liudongtushuguan.cn.key');
var cert = fs.readFileSync('./key/1_www.liudongtushuguan.cn_bundle.crt');

var options = {
	key : key,
	cert : cert,
};

app.use(cookieParser());
app.use(bodyParser.json());

const httpsServer = https.createServer(options,app);
httpsServer.listen(443,() =>{
    console.log('listening 443 port');
});

socket(httpsServer);  //websocket

app.use(router);

小程序规定必须要用https协议。

  • websocket模块

这里使用ws模块而没有选择使用socket.io,因为小程序客户端不支持socket.io

const WebSocket = require('ws');  //使用ws模块
const queryString = require('querystring');
const URL = require('url');
const sessions = require('./session');



module.exports = (httpServer) => {

	const wss = new WebSocket.Server({server : httpServer});

	wss.on('connection',(ws, req) => {
			let sessionId = queryString.parse(URL.parse(req.url).query).sessionId;
			ws.id = sessionId;
    	    ws.on('message' , (msg) => {
    	    		let msgObj = JSON.parse(msg);
    	    		if(sessions[msgObj.targetId]){
    	    			wss.clients.forEach((client) => {
    					if(client.id === msgObj.targetId){
    						let data = {
    							time : msgObj.time,
    							borrower :msgObj.nickName, 
    							book :msgObj.bookName,
    							borrowerId : sessions[sessionId],
    							bookId : msgObj.bookId,
    							wxNum : msgObj.wxNum,
    							phoneNum : msgObj.phoneNum,
    							msg : msgObj.msg
    						};
    						client.send(JSON.stringify(data));
    					}
    				});
    	    		}
    	    });
	});
};
  • mongoose操作数据库

db.js:

const mongoose = require('mongoose');

mongoose.connect('mongodb://app:[email protected]/wxapp');

const connection = mongoose.connection;
connection.once('open', (err) => {
	if(err){
		console.log('Database connection failure');
	}else{
		console.log('Database opened');

	}
});

connection.on('error',(err) => {
	console.log('Mongoose connected error '+ err);
});

connection.on('disconnected', () => {
	console.log('Mongoose disconnected');
});

module.exports = {
	connection : connection,
	mongoose : mongoose,
};

model.js:

const Schema = mongoose.Schema;

const UserSchema = new Schema({
	onlyId : {type: String},
	publishedBooks : {type: Array},
	borrowedBooks : {type: Array},
	borrowMessages : Array, 
});

const BookSchma = new Schema({
	isbn : String,
	title : String,
	press : String,
	author : String,
	rate : String,
	tags : String,
	image : String,
	status : {type : Boolean,default : true},
	ownerId : String,
	owner : String,
	ownerImage : String,
});

const userModel =  mongoose.model('user' , UserSchema);
const bookModel = mongoose.model('book' , BookSchma);

module.exports = {
	UserModel  : userModel,
	BookModel : bookModel,
}
  • 获得微信用户的openId

小程序前端会请求微信服务器得到一个code, 将code发送给自己的服务器,然后自己的服务器给微信服务器发送请求,得到微信用户的唯一标识openId

const https = require('https');
const queryString = require('querystring');

const sessions = require('./session');


module.exports = (req, res, next) => {
		let code = req.query.code;
		let otherRes = res;
		DATA.js_code = code;
		OPTION.path = PATH + queryString.stringify(DATA);
		let wxReq = https.request(OPTION, (res) => {
			if(res.statusCode == 200){
				let json = '';
				res.on('data' , (data) => {
					json+=data;
				});
				res.on('end' , () => {
					json =JSON.parse(json);
					let openId = json.openid;
					sessions[openId] = openId;
					otherRes.type('application/json');
					otherRes.json({
						data : {'sessionId' : openId},
					});
					otherRes.status(200);
				});
			}
		});
		wxReq.end();
};

使用pm2部署

安装pm2

npm install  -g pm2

启动应用

pm2 start app.js

总结

流动图书馆小程序是由三个人的小团队设计和开发的。我主要负责前后端的开发工作。这对从来没接触过服务端和小程序开发的我来说是一个挑战当然也是一次难得的学习机会。从最初对小程序,服务端两眼一抹黑,到搭建出应用的雏形,实现基本的效果,再到最后的拆分整合代码,这期间经历了很多,也收获了很多。从数据库的设计到后台数据库操作、会话管理、http服务接口一直到前后端数据交互、小程序前端,我对程序开发的大致流程有了更进一步的了解,弥补的之前对服务端知识的缺失。

三个人的小团队,因为兴趣结在一起,利用工作之余的时间完成自己喜欢的事情真的是一件令人十分有成就感的事情。

有兴趣的可以看源码

小程序前端

小程序后端

如果觉得不错,就毫不吝啬地给个star吧。后期项目还会继续更新和完善。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].