如何在Node.js中处理多个端点

时间:2019-05-04 12:25:13

标签: node.js sap-conversational-ai

我正在开发SAP Conversational AI机器人,以通过单个webhook.js文件处理登录到SAP和产品查询,该文件始终会命中/ login部分,我想修改此文件(presales_assistance_webhook.js)以处理< / strong>,多个帖子支持,并且由于SAP会话AI的限制而无法使用多个文件。我是nodejs的新手,为此需要一些专家支持。

// 1.login意图:app.post('/ login',(req,res)=> {})

// 2。产品询价意图:app.post('/ presales',(req,res)=> {})

presales_assistance_webhook.js

const express = require('express')
const bodyParser = require('body-parser')

const app = express()
const port = process.env.PORT || 5000
app.use(bodyParser.json())

app.post('/login', (req, res) => {
  console.log(req.body)


  res.send({
    replies: [{
      type: 'text',
      content: 'Welcome to b1 system !',
    }],
    conversation: {
      memory: {
        key: 'value'
      }
    }
  })
})
app.post('/presales', (req, res) => {
  console.log(req.body)


  res.send({
    replies: [{
      type: 'text',
      content: 'Output ok',
    }],
    conversation: {
      memory: {
        key: 'value'
      }
    }
  })
})

app.post('/errors', (req, res) => {
  console.log(req.body)
  res.send()
})

app.listen(port, () => {
  console.log(`Server is running on port ${port}`)
}) 

2 个答案:

答案 0 :(得分:1)

您做得很好。该代码对我有用。

const express = require('express')
const bodyParser = require('body-parser')

const app = express()
const port = process.env.PORT || 5000
app.use(bodyParser.json())

app.post('/login',getLoginInformations);
app.post('/presales',getPresalesInformations);

function getLoginInformations(req, res) {  
  console.log(req.body) 
  res.send({
    replies: [{
      type: 'text',
      content: 'Welcome to b1 system !'
    }],
    conversation: {
      memory: {
        key: 'value'
      }
    }
  })
}

function getPresalesInformations(req, res) {  
  console.log(req.body) 
  res.send({
    replies: [{
      type: 'text',
      content: 'Output ok'
    }],
    conversation: {
      memory: {
        key: 'value'
      }
    }
  })
} 
app.post('/errors', (req, res) => {
  console.log(req.body)
  res.send()
})

app.listen(port, () => {
  console.log(`Server is running on port ${port}`)
})

答案 1 :(得分:0)

根据this的答案,您可以简单地使用

    app.get(
    ['/test', '/alternative', '/barcus*', '/farcus/:farcus/', '/hoop(|la|lapoo|lul)/poo'],
    function ( request, response ) {

    }
);