Last active
January 16, 2022 10:18
-
-
Save kuc-arc-f/7c211310720653415039048c208bc9cb to your computer and use it in GitHub Desktop.
Serverless Framework + express, mongoDB atlas sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.getConfig = () => { | |
//console.log("IS_OFFLINE= ", process.env.IS_OFFLINE); | |
return { | |
MONGODB_URL: '', | |
MONGODB_DB_NAME: 'test', | |
DELETE_TOKEN_EXPIRE_MIN : 120, // min (ex: 120) | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var router = express.Router(); | |
const Config = require('../config'); | |
const LibMongo = require('../lib/LibMongo'); | |
/* GET home page. */ | |
router.get('/', function(req, res, next) { | |
console.log("root"); | |
res.json({ret: 'root OK'}); | |
}); | |
router.get('/test', function(req, res, next) { | |
console.log("test"); | |
res.json({ret: 'OK'}); | |
}); | |
router.get('/test2',async function(req, res, next) { | |
//console.log("test2"); | |
const cfg = Config.getConfig(); | |
//console.log( cfg.MONGODB_URL ); | |
//console.log( cfg.MONGODB_DB_NAME ); | |
const client = await LibMongo.getClient(); | |
const db = client.db(cfg.MONGODB_DB_NAME); | |
const collection = db.collection("tasks"); | |
const items = await collection.find({}).sort({created_at: -1}).toArray(); | |
client.close(); | |
//console.log(items); | |
res.json({data: items}); | |
}); | |
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const MongoClient = require('mongodb').MongoClient; | |
const Config = require('../config.js'); | |
const CFG = Config.getConfig(); | |
//console.log( CFG.MONGODB_URL ); | |
//console.log( CFG.MONGODB_DB_NAME ); | |
const URI = CFG.MONGODB_URL + "?retryWrites=true&w=majority"; | |
const DbName = CFG.MONGODB_DB_NAME; | |
module.exports.getClient = async () => { | |
// console.log("IS_OFFLINE= ", process.env.IS_OFFLINE); | |
try{ | |
//console.log(process.env.MONGODB_DB_NAME); | |
const client = await new MongoClient(URI, { useNewUrlParser: true, useUnifiedTopology: true }); | |
await client.connect(); | |
return client | |
} catch (err) { | |
console.log(err); | |
throw new Error('Error, getClient'); | |
} | |
}; | |
/* | |
module.exports.getArrayWhere = async (collectionName , where) => { | |
console.log("IS_OFFLINE= ", process.env.IS_OFFLINE); | |
try{ | |
let client = await MongoClient.connect(URL); | |
const db = client.db(DbName); | |
const collection = db.collection(collectionName); | |
const items = await collection.find(where).sort({created_at: -1}).toArray() | |
client.close(); | |
return items | |
} catch (err) { | |
console.log(err); | |
throw new Error('Error, getClient'); | |
} | |
}; | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment