Thank you, this is my index.js:
const express = require("express")
const cors = require('cors')
const Moralis = require("moralis").default
const { EvmChain } = require("@moralisweb3/evm-utils")
const app = express()
const port = 8080
// Use CORS
app.use(cors())
// Using express.urlencoded middleware
app.use(express.urlencoded({
extended: true
}))
// Values set in the backend
const MORALIS_API_KEY = "EDITED OUT" //FILL THIS WITH YOUR KEY!!
const NETWORK = "evm";
// Put your preferred message values here
const DOMAIN = 'moralis.io';
const STATEMENT = 'Please sign this message to confirm your identity.';
const URI = 'https://moralis.io/';
const EXPIRATION_TIME = '2023-01-01T00:00:00.000Z';
const TIMEOUT = 15;
// requestMessage method
async function requestMessage(address, chain) {
const result = await Moralis.Auth.requestMessage({
address,
chain,
network: NETWORK,
domain: DOMAIN,
statement: STATEMENT,
uri: URI,
expirationTime: EXPIRATION_TIME,
timeout: TIMEOUT,
})
return result
}
// verify method
async function verify(message, signature) {
const verifiedData = Moralis.Auth.verify({
message: message,
signature: signature,
network: NETWORK,
})
return verifiedData
}
// POST operation for requestMessage()
app.post("/request", async (req, res) => {
try {
const data = await requestMessage(req.body.address, req.body.chain)
res.status(200)
res.json(data)
} catch (error) {
// Handle errors
console.error(error)
res.status(500)
res.json({ error: error.message })
}
})
// POST operation for verify()
app.post("/verify", async (req, res) => {
try {
const data = await verify(req.body.message, req.body.signature)
res.status(200)
res.json(data)
} catch (error) {
// Handle errors
console.error(error)
res.status(500)
res.json({ error: error.message })
}
})
// getNativeBalance method
async function getNativeBalance(address, chain) {
const nativeBalance = await Moralis.EvmApi.balance.getNativeBalance({
address,
chain,
})
const native = nativeBalance.result.balance.ether
return native
}
// POST operation for getNativeBalance()
app.post("/nativeBalance", async (req, res) => {
try {
// Get and return the native balance
const nativeBalance = await getNativeBalance(req.body.address, req.body.chain)
res.status(200)
res.send(nativeBalance)
} catch (error) {
// Handle errors
console.error(error)
res.status(500)
res.json({ error: error.message })
}
})
// Default GET operation
app.get("/", (req, res) => {
res.send("Server running!")
})
const startServer = async () => {
await Moralis.start({
apiKey: MORALIS_API_KEY,
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
}
startServer()
And this is my package.json:
{
"name": "nodejs-cloud-run-hello-world",
"version": "0.0.1",
"description": "Hello World application for Cloud Run",
"main": "index.js",
"scripts": {
"start": "node index.js",
"lint": "eslint **.js",
"fix": "eslint --fix *.js",
"test": "mocha test/*.test.js --check-leaks --timeout 5000"
},
"author": "Google LLC",
"license": "0BSD",
"engines": {
"node": ">=11.14"
},
"dependencies": {
"express": "^4.17.1",
"handlebars": "^4.7.4"
},
"devDependencies": {
"eslint": "8.29.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-promise": "6.1.1",
"gaxios": "5.0.2",
"mocha": "10.1.0",
"prettier": "2.8.1"
}
}
Not sure if it is relevant, but this is my VS Code launch.json:
{
"configurations": [
{
"name": "Cloud Run: Run/Debug Locally",
"type": "cloudcode.cloudrun",
"request": "launch",
"build": {
"buildpacks": {
"path": "package.json",
"builder": "gcr.io/buildpacks/builder:v1"
}
},
"image": "google-moralis-functions",
"service": {
"name": "google-moralis-functions",
"containerPort": 8080,
"resources": {
"limits": {
"memory": "256Mi"
}
}
},
"target": {
"minikube": {}
},
"watch": true
}
]
}