Good Afternoon, I have a react app hosted on Netlify and have converted my Expressjs API backend to be serverless for use with Netlify functions. Right now I am running into the below error:
502: ReferenceError: regeneratorRuntime is not defined", " at /var/task/server.js"
Server.js is the main API file that imports my middleware. I have installed the babel regnerator plugin and added it to my babel file as well as importing it on the top of my Server.js file. I am not sure what else to do as I am new to the netlify platform.
Server.js
import "core-js/stable";
import "regenerator-runtime/runtime";
const express = require('express');
var expressSession = require('express-session');
const cors = require('cors');
const path = require('path');
const app = express();
const axios = require('axios');
const mongoose = require('mongoose');
const rateLimit = require("express-rate-limit");
const serverless = require('serverless-http');
require('dotenv').config()
// Middleware
const saveLoggedInUserMiddleware = require('../Middleware/SaveLoggedInUserMiddleware');
const hostValidationMiddleware = require('../Middleware/HostValidationMiddleware');
const sessionValidationMiddleware = require('../Middleware/SessionValidationMiddleware');
const updateUserDataMiddleware = require('../Middleware/UpdateUserDataMiddleware');
//Routers
const StoreInvoiceRouter = require('../Routes/Store-Update-Invoices');
const UserInvoicesRouter = require('../Routes/GetUserInvoices');
const UpdateUserProfileRouter = require('../Routes/UpdateUserProfile');
async function connectToDB() {
// Database
await mongoose.connect(process.env.mongo_url, { useNewUrlParser: true, useUnifiedTopology: true }, () => {
console.log('[connectToDB]: Connected to DB');
})
}
connectToDB();
// Implemented a rate limiter which is a TEMPORARY FIX for the infinite loop by the useEffect
const limiter = rateLimit({
windowMs: .1 * 60 * 1000, // 10 seconds limit
max: 4
});
// Creating the session in order to save user data to req.session
app.use(
expressSession({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
})
);
app.use('/fetchUserInvoices', limiter);
app.use(express.json());
//app.use(cors());
app.use(express.static('./build'));
app.use('/.netlify/functions/', hostValidationMiddleware, sessionValidationMiddleware, StoreInvoiceRouter);
app.use('/.netlify/functions/', saveLoggedInUserMiddleware, UserInvoicesRouter);
app.use('/.netlify/functions/', updateUserDataMiddleware, UpdateUserProfileRouter);
app.get('*', async (req,res) => {
res.sendFile('/Users/augustshah/Documents/Coding-Tools-new/Projects/Payment-Dashboard/build/index.html');
});
// Function makes call to endpoint that runs deletion login, this allows the client to only have to be refreshed once to
// render changes on the frontend.
async function updateDBCall() {
const url = `${process.env.REACT_APP_STORE_INVOICES}`;
const axiosConfig = {
method: 'get',
url
};
await axios(axiosConfig).catch((e) => {console.log(e)});
console.log('[updateDBCall]: Executed call to STORE_INVOICES')
};
// Supporting functions
updateDBCall();
app.listen(process.env.PORT || 8080, () => {
console.log(`Server listening on 8080`);
});
module.exports.handler = serverless(app);
babel.config.js
module.exports = api => {
api.cache.forever();
return {
presets: [
"@babel/preset-env",
"@babel/preset-react"
],
plugins: [
"add-react-displayname",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/transform-runtime"
]
};
}
Changed my babel to the below as well as installed and implemented netlify-lambda
module.exports = api => {
api.cache.forever();
return {
"presets": [
[ "@babel/preset-env", {
"targets": {
"node": "8.10"
}
}]
],
plugins: [
"add-react-displayname",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread"
]
};
}