Skip to content

Commit

Permalink
firt commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bailicangdu committed Mar 24, 2017
1 parent 027f226 commit b4d57ed
Show file tree
Hide file tree
Showing 25 changed files with 10,471 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# node-elm
nodejs 构筑外卖后台系统,先占坑,后续更新

# 说明

> nodejs + mongodb 构建的外卖平台后台系统
> 目的是构建一个横跨 前端 + 后台 + 原生IOS + 原生Android 的完整电商系统。
> 目前为测试版,如果对您对此项目有兴趣,可以点 "Star" 支持一下 谢谢! ^_^
> 前端项目已经完成。[地址在这里](https://github.com/bailicangdu/vue2-elm)
> 原生移动端项目仍在准备阶段,过一段时间会出一个先行体验版。[地址在这里](https://github.com/bailicangdu/RN-elm)

71 changes: 71 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const express = require('express');
const db = require('./lib/db.js');
const formidable = require('formidable');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const ObjectId = require('mongodb').ObjectID;
const session = require('express-session');
const crypto = require('crypto');
const app = express();


app.set('view engine', 'ejs');
app.use(express.static('./public'));
// app.use(cookieParser());
app.use(session({
secret: 'keyboard cat',
name: 'UID',
cookie: {maxAge: 9999999999, httpOnly: true},
resave: false,
saveUninitialized: true
}));

app.get('/list', async (req, res) => {
const page = parseInt(req.query.page) || 0;
const limit = 10;
const skip = page*limit;
const docs = await db.find('liuyanban', {}, {limit, skip, sort: {date: -1}});
const count = await db.getCount('liuyanban', {});
res.render('lyb', {
datalist: docs,
count: Math.ceil(count/10)
});
});

app.post('/insert', (req, res) => {
const form = new formidable.IncomingForm();
form.parse(req, async (err, fields, files) => {
try{
await db.insertMany('liuyanban', [fields]);
res.json({status: 200, message: '发表成功'});
}catch(err){
res.json(err);
}
})
});

app.get('/delete', async (req, res) => {
const _id = req.query._id;
try{
const result = await db.deleteMany('liuyanban', {_id: ObjectId(_id)});
res.json({message: '删除成功',result});
}catch(err){
res.json({message: '删除失败'});
}
});

app.get('/login', (req, res) => {
res.render('login');
});

app.post('/signin', (req, res) => {
console.log(req.session.password);
const form = new formidable.IncomingForm();
form.parse(req, async (err, fields, files) => {
if (!req.session.password) {
req.session.password = fields.password;
}
});
});

app.listen(3000);
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("babel-core/register");
require("./app.js");
3 changes: 3 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
url: 'mongodb://localhost:27017/lyb',
}
73 changes: 73 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const config = require('./config.js');

const _connectDB = () => new Promise(async (reslove, reject) => {
try{
const db = await MongoClient.connect(config.url);
reslove(db);
}catch(err){
reject('连接数据库失败');
}
});

exports.find = (collectionName, filter = {}, json = {}) => new Promise(async (reslove, reject) => {
const db = await _connectDB();
const {limit = 0, skip = 0, sort = {} } = json;
const collection = db.collection(collectionName);
const datalist = collection.find(filter).limit(limit).skip(skip).sort(sort);
try{
const docs = await datalist.toArray();
reslove(docs);
}catch(err){
reject('查找失败');
}
db.close();
});

exports.insertMany = (collectionName, arr) => new Promise(async (resolve, reject) => {
const db = await _connectDB();
const collection = db.collection(collectionName);
try{
const result = collection.insertMany(arr);
resolve(result);
}catch(err){
reject('添加失败');
}
db.close();
});

exports.deleteMany = (collectionName, filter) => new Promise(async (resolve, reject) => {
const db = await _connectDB();
const collection = db.collection(collectionName);
try{
const result = collection.deleteMany(filter);
resolve(result)
}catch(err){
reject('删除失败');
}
db.close();
});

exports.updateMany = (collectionName, filter, newData) => new Promise(async (resolve, reject) => {
const db = await _connectDB();
const collection = db.collection(collectionName);
try{
const result = collection.updateMany(filter, newData);
resolve(result);
}catch(err){
reject('修改失败');
}
db.close();
});

exports.getCount = (collectionName, fitlter) => new Promise(async (resolve, reject) => {
const db = await _connectDB();
const collection = db.collection(collectionName);
try{
const count = collection.find(fitlter).count();
resolve(count);
}catch(err){
reject('获取数据失败');
}
});
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "test",
"version": "0.0.1",
"description": "test",
"main": "index.js",
"scripts": {
"dev": "supervisor --harmony index.js"
},
"repository": {
"type": "git",
"url": "sdf"
},
"keywords": [
"sdf"
],
"author": "sdfsd",
"license": "ISC",
"dependencies": {
"babel-core": "^6.24.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-3": "^6.22.0",
"body-parser": "^1.17.1",
"cookie-parser": "^1.4.3",
"ejs": "^2.5.6",
"express": "^4.15.2",
"express-session": "^1.15.1",
"formidable": "^1.1.1",
"mongodb": "^2.2.25",
"supervisor": "^0.12.0"
}
}
Loading

0 comments on commit b4d57ed

Please sign in to comment.