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
// Import required dependencies | |
const express = require("express"); | |
const multer = require("multer"); | |
const path = require("path"); | |
const fs = require("fs"); |
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
// Specifies the file upload location | |
const diskStorage = multer.diskStorage({ | |
destination: (req, file, cb) => { | |
cb(null, path.join(__dirname, "uploads")); | |
}, | |
filename: (req, file, cb) => { | |
cb( | |
null, | |
`${file.fieldname}-${Date.now()}${path.extname(file.originalname)}` | |
); |
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
const acceptedFileTypes = [ | |
"image/jpeg", | |
"image/png", | |
"image/svg+xml", | |
"video/mp4", | |
"video/quicktime", | |
"video/webm", | |
"application/pdf", | |
"application/msword", | |
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
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
// Create an express app | |
const PORT = 5000; // Best practice use environment variable | |
const HOST = "http://localhost"; // Best practice use environment variable | |
const app = express(); | |
app.use(express.json()); | |
app.post("/uploads", upload.single("file"), (req, res) => { | |
const file = req.file; | |
if (!file) { |
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
// import required dependencies | |
import dotenv from 'dotenv'; | |
import nodemailer from 'nodemailer'; | |
import { google } from 'googleapis'; | |
const OAuth2 = google.auth.OAuth2; | |
// init dotenv | |
dotenv.config(); | |
// get environment variables |
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
// create OAuth2 client | |
const oauth2Client = new OAuth2( | |
OAUTH_CLIENT_ID, | |
OAUTH_CLIENT_SECRET, | |
'https://developers.google.com/oauthplayground' | |
); | |
// set refresh token | |
oauth2Client.setCredentials({ | |
refresh_token: OAUTH_REFRESH_TOKEN |
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
// create reusable transporter object using the default SMTP transport | |
const transporter = nodemailer.createTransport({ | |
service: 'gmail', | |
auth: { | |
type: 'OAuth2', | |
user: OAUTH_EMAIL, | |
clientId: OAUTH_CLIENT_ID, | |
clientSecret: OAUTH_CLIENT_SECRET, | |
refreshToken: OAUTH_REFRESH_TOKEN, | |
accessToken: accessToken.toString() |
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
589228529 |
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
39533366 |