CodeGenieApp / CodeGenieApp/serverless-express
How to use Async/await?
- Lingua principale
- JavaScript
- Stelle
- 5.3k
- Fork
- 676
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
I read https://github.com/vendia/serverless-express/issues/481 https://github.com/vendia/serverless-express/issues/134 and https://github.com/vendia/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/src/lambda-async-setup.js but I still cannot figure out how to connect to a database (postgres, not from RDS). A minimal example of what I'm trying to do:
```
//lambda.js
require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')
exports.handler = serverlessExpress({ app })
//app.js
require('dotenv').config();
const { Client } = require('pg') //leaving out the rest of the libraries
router.post('/createUser', async function (req, res) {
await sqlRequest(`INSERT INTO users ("name", "age" "ect..") VALUES ( ${req.body.name}, ${req.body.age}, ${req.body.ect..})`);
res.status(200).send("inserted!");
})
router.post('/getUser', async function (req, res, next) {
let result = sqlRequest('SELECT * FROM users WHERE id = ' + req.body.id) //all user input is escaped in full version
res.status(200).json(result);
})
async function sqlRequest(sql, params){
const client = new Client(process.env.DATABASE_URL); //this is a postgres URL
await client.connect();
let result = await client.query(sql);
client.end();
return result.rows;
}
module.exports = app
```
The full app is a few hundred lines with many more queries and works fine locally. I believe the issue is being caused by async/await. Is there any way to do this in lambda? If this isn't the right library to use, can someone point me in the right direction? Would you recommend I switch to Azure or some other service to make it easier? Any advice on how to proceed is appreciated.
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Inizia da examples/basic-starter-api-gateway-v2/src/lambda-async-setup.js e confrontalo con gli snippet lambda.js e app.js nell’issue. Determina se il repository dovrebbe documentare un esempio minimo di connessione a PostgreSQL per gli handler Express asincroni e definisci come completato un esempio chiaro e funzionante che spieghi come le query al database si inseriscono nella configurazione Lambda.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- aws, express, javascript, node.js, postgresql
- Ambito
- api, backend, cloud, databases
- Tipo di issue
- Documentazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100