Encountering error ReferenceError: regeneratorRuntime is not defined for Expressjs API on netlify
Encountering error ReferenceError: regeneratorRuntime is not defined for Expressjs API on netlify
下午好,
我有一个托管在 Netlify 上的 React 应用程序,并将我的 Expressjs API 后端转换为无服务器,以便与 Netlify 函数一起使用。现在我 运行 遇到以下错误:
502: ReferenceError: regeneratorRuntime is not defined", " at /var/task/server.js"
Server.js 是导入我的中间件的主要 API 文件。
我已经安装了 babel regnerator 插件并将其添加到我的 babel 文件中,并将其导入到我的 Server.js 文件的顶部。我不知道还能做什么,因为我是 netlify 平台的新手。
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"
]
};
}
将我的 babel 更改为以下内容,并安装并实施了 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"
]
};
}
下午好, 我有一个托管在 Netlify 上的 React 应用程序,并将我的 Expressjs API 后端转换为无服务器,以便与 Netlify 函数一起使用。现在我 运行 遇到以下错误:
502: ReferenceError: regeneratorRuntime is not defined", " at /var/task/server.js"
Server.js 是导入我的中间件的主要 API 文件。 我已经安装了 babel regnerator 插件并将其添加到我的 babel 文件中,并将其导入到我的 Server.js 文件的顶部。我不知道还能做什么,因为我是 netlify 平台的新手。
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"
]
};
}
将我的 babel 更改为以下内容,并安装并实施了 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"
]
};
}