[英]Datastax Astra netlify and react-app, should I use nodejs client or REST API for serverless functions?
I built a simple react app with "create-react-app" and I want to use serverless functions with netlify.我用“create-react-app”构建了一个简单的反应应用程序,我想在 netlify 中使用无服务器功能。 I use DataStax Astra Cassandra DB for that purpose, and created a netlify.toml config and.env variables (for the Database) inside my react project.
为此,我使用 DataStax Astra Cassandra DB,并在我的 react 项目中创建了一个 netlify.toml 配置和 .env 变量(用于数据库)。
I set up a serverless functions folder for netlify:我为 netlify 设置了一个无服务器函数文件夹:
const { createClient } = require('@astrajs/collections')
// create an Astra client
exports.handler = async function (event, context) {
try {
const astraClient = await createClient({
astraDatabaseId: process.env.ASTRA_DB_ID,
astraDatabaseRegion: process.env.ASTRA_DB_REGION,
applicationToken: process.env.ASTRA_DB_APPLICATION_TOKEN,
})
// const basePath = `/api/rest/v2/KEYSPACES/${process.env.ASTRA_DB_KEYSPACE}/collections/messages`
const messagesCollection = astraClient
.namespace(process.env.ASTRA_DB_KEYSPACE)
.collection('messages')
const message = await messagesCollection.create('msg1', {
text: 'hello my name is Marc!',
})
return {
statusCode: 200,
body: JSON.stringify(message),
}
} catch (e) {
console.error(e)
return {
statusCode: 500,
body: JSON.stringify(e),
}
}
it works when I run netlify dev
, then my.env variables are injected into the.js file.它在我运行
netlify dev
时起作用,然后将 my.env 变量注入到 .js 文件中。 However, I am wondering if I should use the nodejs datastax
collections here, or the REST API
functions from datastax ( https://docs.datastax.com/en/astra/docs/astra-collection-client.html )? However, I am wondering if I should use the
nodejs datastax
collections here, or the REST API
functions from datastax ( https://docs.datastax.com/en/astra/docs/astra-collection-client.html )? Because with react, it's essentially running in the browser or not?因为有了反应,它本质上是在浏览器中运行的吗? I am wondering why this still works with nodejs (because its not a nodejs environment with react, or is it?) I am getting access to my functions via
localhost:8888/.netlify/functions/functionName
is this served from a nodejs server or is it browser stuff?我想知道为什么这仍然适用于 nodejs(因为它不是一个带有 react 的 nodejs 环境,或者是吗?)我通过
localhost:8888/.netlify/functions/functionName
访问我的函数是从 nodejs 服务器或是浏览器的东西吗?
it works when I run netlify dev, then my.env variables are injected into the.js file.
它在我运行 netlify dev 时起作用,然后将 my.env 变量注入到 .js 文件中。 However, I am wondering if I should use the nodejs datastax collections here, or the REST API functions from datastax ( https://docs.datastax.com/en/astra/docs/astra-collection-client.html )?
However, I am wondering if I should use the nodejs datastax collections here, or the REST API functions from datastax ( https://docs.datastax.com/en/astra/docs/astra-collection-client.html )? Because with react, it's essentially running in the browser or not?
因为有了反应,它本质上是在浏览器中运行的吗?
Correct - you would expose your Astra credentials to the world if you connect to your database directly from your React app.正确 - 如果您直接从您的 React 应用程序连接到您的数据库,您将向世界公开您的 Astra 凭据。
I am wondering why this still works with nodejs (because its not a nodejs environment with react, or is it?) I am getting access to my functions via localhost:8888/.netlify/functions/functionName is this served from a nodejs server or is it browser stuff?
我想知道为什么这仍然适用于 nodejs (因为它不是一个带有 react 的 nodejs 环境,或者是吗?)我通过 localhost:8888/.netlify/functions/functionName 访问我的函数是从 nodejs 服务器提供的还是是浏览器的东西吗?
Netlify functions run serverside so it is safe to connect to Astra in your function code. Netlify 函数在服务器端运行,因此在您的 function 代码中连接到 Astra 是安全的。 Here's an example: https://github.com/DataStax-Examples/todo-astra-jamstack-netlify/blob/master/functions/createTodo.js
这是一个示例: https://github.com/DataStax-Examples/todo-astra-jamstack-netlify/blob/master/functions/createTodo.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.