Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by max on 2017/6/5.
- */
- const Koa = require('koa');
- const Router = require('koa-router');
- const bodyParser = require('koa-bodyparser');
- const uuid = require('node-uuid');
- const mongodb = require('mongodb');
- const app = new Koa();
- const router = Router();
- const mongodbServer = new mongodb.Server('localhost', 27017, { auto_reconnect: true, poolSize: 10 });
- const db = new mongodb.Db('Library', mongodbServer);
- app.use(bodyParser({
- 'formLimit': '50mb',
- 'jsonLimit': '50mb',
- 'textLimit': '50mb'
- }));
- // 設定根路徑的處理函數
- router.get('/', async function(ctx) {
- ctx.body = 'Hello World';
- });
- // login
- router.use('/api/login', async function(ctx, next) {
- ctx.set('Access-Control-Allow-Origin', '*');
- ctx.set('Access-Control-Allow-Headers', 'Content-Type');
- await next()
- });
- router.options('/api/login', async function(ctx) {
- ctx.body = { code: 200, msg: '登入成功' };
- });
- router.post('/api/login', async function(ctx) {
- const username = await ctx.request.body.username;
- const password = await ctx.request.body.password;
- var user = {
- id: 1,
- username: 'admin',
- password: '123456',
- avatar: 'https://raw.githubusercontent.com/taylorchen709/markdown-images/master/vueadmin/user.png',
- name: '张某某'
- };
- user = JSON.parse(JSON.stringify(user));
- console.log(`username: ${username}, password: ${password}`);
- ctx.body = { code: 200, msg: '登入成功', user };
- });
- // martial classic classification
- router.use('/api/martial/classic/classifications', async function(ctx, next) {
- ctx.set('Access-Control-Allow-Origin', '*');
- ctx.set('Access-Control-Allow-Headers', 'Content-Type');
- await next()
- });
- router.options('/api/martial/classic/classifications', async function(ctx) {
- ctx.body = { code: 200, msg: '文章新增成功' };
- });
- router.post('/api/martial/classic/classifications', async function(ctx) {
- const title = await ctx.request.body.title;
- const secondTitle = await ctx.request.body.secondTitle;
- const imageGroup = await ctx.request.body.imageGroup;
- db.open(function() {
- db.collection('Martial', function(err, collection) {
- var uuidBinary = new Buffer(uuid.v1({}, []));
- var id = mongodb.Binary(uuidBinary, mongodb.Binary.SUBTYPE_UUID);
- /* Generate Timestamp and convert for mongodb */
- var ts = new Date().getTime();
- var i = ts % 1000;
- var t = new mongodb.Timestamp(i, Math.floor(ts * 0.001));
- /* Insert a data with uuid */
- collection.update(
- {
- pageName: "classic"
- },
- {
- pageName: 'classic',
- pageData: {
- title: title,
- secondTitle: secondTitle,
- imageGroup: imageGroup,
- lastTime: t
- }
- },
- {
- upsert: true
- },
- function(err, data) {
- if (data) {
- console.log('Successfully Update');
- db.close();
- } else {
- console.log('Failed to Update');
- }
- }
- );
- });
- });
- ctx.body = { code: 200, msg: '文章新增成功' };
- });
- router.get('/api/martial/classic/classifications', async function(ctx) {
- const data = await getClassic();
- ctx.body = { code: 200, msg: '資料讀取成功', data: data };
- });
- async function getClassic () {
- return new Promise(function(resolve, reject) {
- db.open(function() {
- db.collection('Martial', function(err, collection) {
- collection.find(
- {
- "pageName": "classic"
- },
- function(err, data) {
- if (data) {
- const dataArr = data.toArray();
- console.log('Successfully Update');
- db.close();
- resolve(dataArr);
- } else {
- console.log('Failed to Update');
- }
- }
- );
- });
- });
- });
- }
- router.get('/api/classifications/name', async function(ctx) {
- let data = [];
- let classicObject = await getClassificationsName( 'classic' );
- let classicResult = await formatClassificationsObject( classicObject );
- data.push(classicResult);
- ctx.body = { code: 200, msg: '資料讀取成功', data: data };
- });
- async function getClassificationsName ( pageName ) {
- return new Promise(function(resolve, reject) {
- db.open(function() {
- db.collection('Martial', function(err, collection) {
- collection.find(
- {
- "pageName": pageName
- },
- {
- "pageData.imageGroup.src": 0
- },
- function(err, data) {
- if (data) {
- const dataArr = data.toArray();
- db.close();
- resolve(dataArr);
- console.log('Successfully Update');
- } else {
- console.log('Failed to Update');
- }
- }
- );
- });
- });
- });
- }
- async function formatClassificationsObject ( object ) {
- return new Promise(function (resolve, reject) {
- let result;
- let classificationsList = [];
- let pageName = object[0].pageName;
- object[0].pageData.imageGroup.forEach( function( item ) {
- classificationsList.push( item['first_title'] );
- });
- result = {
- 'pageName': pageName,
- 'classifications': classificationsList
- };
- resolve( result );
- });
- }
- app.use(router.routes());
- app.listen(3001, function() {
- console.log('Listening on port 3001');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement