put boards in a map

merge-requests/208/head
fatchan 5 years ago
parent 91e828e768
commit 9792f123d5
  1. 16
      db/boards.js
  2. 3
      models/forms/make-post.js
  3. 2
      server.js

@ -1,7 +1,8 @@
'use strict'; 'use strict';
const Mongo = require(__dirname+'/db.js') const Mongo = require(__dirname+'/db.js')
, db = Mongo.client.db('jschan'); , db = Mongo.client.db('jschan')
, boardCache = new Map();
module.exports = { module.exports = {
@ -11,7 +12,7 @@ module.exports = {
return db.collection('boards').findOne({ '_id': name }); return db.collection('boards').findOne({ '_id': name });
}, },
find: (name) => { find: () => {
return db.collection('boards').find({}).toArray(); return db.collection('boards').find({}).toArray();
}, },
@ -31,9 +32,18 @@ module.exports = {
return db.collection('boards').deleteMany({}); return db.collection('boards').deleteMany({});
}, },
cache: async () => {
const boards = await module.exports.find();
for (let i = 0; i < boards.length; i++) {
const board = boards[i];
boardCache.set(board._id, board);
}
},
exists: async (req, res, next) => { exists: async (req, res, next) => {
const board = await module.exports.findOne(req.params.board); //const board = await module.exports.findOne(req.params.board);
const board = boardCache.get(req.params.board);
if (!board) { if (!board) {
return res.status(404).render('404'); return res.status(404).render('404');
} }

@ -12,7 +12,7 @@ const uuidv4 = require('uuid/v4')
, simpleMarkdown = require(__dirname+'/../../helpers/markdown.js') , simpleMarkdown = require(__dirname+'/../../helpers/markdown.js')
, sanitize = require('sanitize-html') , sanitize = require('sanitize-html')
, sanitizeOptions = { , sanitizeOptions = {
allowedTags: [ 'span', 'a', 'code', 'em', 'strong' ], allowedTags: [ 'span', 'a', 'em', 'strong' ],
allowedAttributes: { allowedAttributes: {
'a': [ 'href', 'class' ], 'a': [ 'href', 'class' ],
'span': [ 'class' ] 'span': [ 'class' ]
@ -115,7 +115,6 @@ module.exports = async (req, res, next, numFiles) => {
if (Array.isArray(processedFile.geometryString)) { if (Array.isArray(processedFile.geometryString)) {
processedFile.geometryString = processedFile.geometryString[0]; processedFile.geometryString = processedFile.geometryString[0];
} }
files.push(processedFile); files.push(processedFile);
} catch (err) { } catch (err) {

@ -20,6 +20,8 @@ const express = require('express')
// let db connect // let db connect
await Mongo.connect(); await Mongo.connect();
const Boards = require(__dirname+'/db/boards.js');
await Boards.cache();
// parse forms and allow file uploads // parse forms and allow file uploads
app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.urlencoded({extended: true}));

Loading…
Cancel
Save