Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add more API endpoints #5

Open
addreeh opened this issue May 2, 2024 · 0 comments
Open

How to add more API endpoints #5

addreeh opened this issue May 2, 2024 · 0 comments

Comments

@addreeh
Copy link

addreeh commented May 2, 2024

Hi, I would like to know how can I add more endpoints, for example when visiting api/hello, I would like to automate something, for example call to greeting command.
I tried this:

import express, { Request, Response } from 'express'
import { startVercel } from '../src'
import { whey, casein } from '../src/scraper/scraper'
import { sendMessage, sendError } from '../src/scraper/telegram'
import { Product } from '../src/interfaces'
import { manage, insert, update } from '../src/scraper/postgres'
import { VercelRequest, VercelResponse } from '@vercel/node'

const app = express()

// enable JSON body parser
app.use(express.json())

// Handle requests to the root URL to check if the server is running
app.get('/', (req: Request, res: Response) => {
  res.send('<h1>Server is running!</h1>')
})

// Handle requests to the root URL for Vercel function and other routes
app.all('*', async (req: Request | VercelRequest, res: Response | VercelResponse, next: Function) => {
  try {
    if ((req as VercelRequest).query !== undefined) {
      // If it's a Vercel request, handle it using the startVercel function
      await startVercel(req as VercelRequest, res as VercelResponse)
    } else {
      // If it's not a Vercel request, continue to the next middleware
      next()
    }
  } catch (e: any) {
    res.status(500).send('<h1>Server Error</h1><p>Sorry, there was a problem</p>')
    console.error(e.message)
  }
})

// Handle requests to /evowhey endpoint
app.get('/evowhey', async (req: Request, res: Response) => {
  try {
    const product: Product = await whey()

    const message = `*HSN |* [${product.product}](${product.link}) *| ${product.price.replace('.', ',')}€*`

    await manage(product).then(async result => {
      if (result.rows.length === 0) {
        await insert(product).then(() => {
          void sendMessage(message)
        }).catch((error: string) => {
          console.log(error)
          void sendError(error)
        })
      } else {
        product.price = '26.10'
        await update(product).then((result) => {
          console.log(result)
          const updatedMessage = `*HSN |* [${product.product}](${product.link}) *|* ${result.originalPrice} *➜ ${product.price.replace('.', ',')}€*${result.message}`
          void sendMessage(updatedMessage)
        }).catch((error: string) => {
          console.log(error)
          void sendError(error)
        })
      }
    })

    res.send(product)
  } catch (error) {
    console.error('Error fetching Evowhey:', error)
    res.status(500).send('Error fetching Evowhey')
  }
})

// Handle requests to /casein endpoint
app.get('/casein', async (req: Request, res: Response) => {
  try {
    const result = await casein()
    res.send(result)
  } catch (error) {
    console.error('Error fetching Casein:', error)
    res.status(500).send('Error fetching Casein')
  }
})

export default app

But its not working, I dont know if maybe I have to add something to vercel.json or any other file.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant