jaeyp / mern-stack-api-vercel

Boilerplate & deployment example for MERN stack API with Vercel (ZEIT Now)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mern-stack-api-vercel

Deployment example for MERN stack API with Vercel (ZEIT Now)

Deploy API through Vercel

  • To connect to MongoDB, you will need your MongoDB connection string. You can get the connection string by clicking "Connect" on the Cluster Overview page within the MongoDB Atlas dashboard, then choosing "Connect your Application": Connect page on MogoDB Atlas dashboard

  • To use your MongoDB connection string without hard-coding it in your project, you can add your connection string to the project as a Secret using the Vercel CLI to keep them secure:

~$ vercel secrets add my-mongodb-uri mongodb+srv://<user>:<password>@my-cluster-uf345.mongodb.net/<database-name>?retryWrites=true

Run Server

~$ yarn start

Test Local Deployment

~$ vercel dev

Deploy Server

~$ vercel
~$ vercel --prod

Other Environments and Configurations

  • Using gmail with Nodemailer

It's important to note that we're using gmail as the service. In order to use it, we will have to turn on the less secure app feature. Setup - Less secure app access

  • Scheduling cron job
┌──────────────── second (optional) 
| ┌────────────── minute 
| | ┌──────────── hour 
| | | ┌────────── day of month 
| | | | ┌──────── month 
| | | | | ┌────── day of week
| | | | | | 
| | | | | |
* * * * * *

//For a cron job to run every second
cron.schedule("* * * * * *", () => {
    //code to be executed
})

//This will run every 10 seconds
cron.schedule("*/10 * * * * *", () => {
    //code to be executed
})

//This will run at the start of every minute
cron.schedule("0 * * * * *", () => {
    //code to be executed
})

//This will run at the start of every hour
cron.schedule("0 * * * *", () => {
    //code to be executed
})

// This will run on 20th of every month at 02:00 hours
cron.schedule("* 02 20 * *", () => {
    //code to be executed
})

References

Create and Deploy a MongoDB-Powered Node.js API with Vercel
Vercel Configuration - routes
Scheduling Cron Jobs in Node.js
Nodemailer - Using Gmail
Google Account Help - Less secure apps & your Google Account

About

Boilerplate & deployment example for MERN stack API with Vercel (ZEIT Now)

License:MIT License


Languages

Language:JavaScript 100.0%