Skip to content

Instantly share code, notes, and snippets.

View dannydenenberg's full-sized avatar
💃
Probably in a musical

Danny Denenberg dannydenenberg

💃
Probably in a musical
View GitHub Profile
app.get("/:user", (req, res) => {
collection.find({ user: req.params.user }).toArray((err, docs) => {
if (err) { // if an error occurred
res.send("An error occured in getting the user info.");
} else {
// there were matches (there are users with that username)
if (docs.length > 0) {
// get the first users password (each user should have a unique username)
let password = docs[0].pass;
app.get("/:user", (req, res) => {
console.log(`Password: ${req.query.pass}`);
});
// The URL for the request.
// Remember that the first route parameter is the username of the user to create
const postURL = 'http://localhost:4567/deb';
// The data will be sent in the `body` property of
// the fetch request and stored with the user data in the database (collection)
const userData = {
// Remember that we don't have to specify the username in the data
// sent with the request because the server inserts the username from the URL into the data sent to the Mongo collection.
pass: '1234' // here is the user password
// The URL for the request.
// Remember that the first route parameter is the username of the user to create
const postURL = 'http://localhost:4567/dan';
// The extra data will be sent in the `body` property of
// the fetch request and stored with the user data in the database (collection)
const extraDataToStore = {
myNewAwesomeData: 'wohooo!',
eyeColor: 'not blue',
hairColor: 'brown---ish',
// The URL for the request.
// Remember that the first route parameter is the username of the user to get information about
const postURL = 'http://localhost:4567/dan';
fetch(postURL, {
method: 'get', // Using get request to recieve database resources
mode: 'cors', // no-cors, cors, *same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
@dannydenenberg
dannydenenberg / testingnodejsmongodbserver.js
Last active July 29, 2019 13:57
For my blog post on MongoDB
// The URL for the request.
// Remember that the first route parameter is the username of the user to create
const postURL = 'http://localhost:4567/dan';
// The extra data will be sent in the `body` property of
// the fetch request and stored with the user data in the database (collection)
const extraDataToStore = {
eyeColor: 'blue',
hairColor: 'brown',
pass: 'mypassword123'
@dannydenenberg
dannydenenberg / nodejsserverwithpathandmongo.js
Last active July 29, 2019 13:57
For my blog article on MongoDB
// init project
const express = require("express"); // the library we will use to handle requests
const mongodb = require("mongodb"); // load mongodb
const port = 4567; // port to listen on
const app = express(); // instantiate express
app.use(require("cors")()); // allow Cross-domain requests
app.use(require("body-parser").json()); // automatically parses request data to JSON
console.log(dimensions.sources);
const dimensions = await page.evaluate(() => {
let sources = []; // an array of the links to each image
document.querySelectorAll(
"#react-root > div > div > div > main > div > div.css-1dbjc4n.r-aqfbo4.r-1niwhzg.r-16y2uox > div > div.css-1dbjc4n.r-14lw9ot.r-1tlfku8.r-1ljd8xs.r-13l2t4g.r-1phboty.r-1jgb5lz.r-1ye8kvj.r-13qz1uu.r-184en5c > div > div > div.css-1dbjc4n.r-1jgb5lz.r-1ye8kvj.r-6337vo.r-13qz1uu > div > section > div > div > div img"
).forEach(img => {
if (img.src) {
sources.push(img)
}
});
document.querySelector(
"#react-root > div > div > div > main > div > div.css-1dbjc4n.r-aqfbo4.r-1niwhzg.r-16y2uox > div > div.css-1dbjc4n.r-14lw9ot.r-1tlfku8.r-1ljd8xs.r-13l2t4g.r-1phboty.r-1jgb5lz.r-1ye8kvj.r-13qz1uu.r-184en5c > div > div > div.css-1dbjc4n.r-1jgb5lz.r-1ye8kvj.r-6337vo.r-13qz1uu > div > section > div > div > div"
);
// the above returns the div for the middle column twitter feed