简体   繁体   中英

Node.js: Send XML response from different files

Having a XML response like:

app.post '/incoming', (req,res) ->
    console.log "Hello, incoming call!"
    message = req.body.Body
    from = req.body.From

    sys.log "From: " + from + ", Message: " + message
    twiml = '<?xml version="1.0" encoding="UTF-8" ?>\n<Response>\n<Say>Thanks for your text, we\'ll be in touch.</Say>\n</Response>'
    res.send twiml, {'Content-Type':'text/xml'}, 200

Can I have different . XML files and res.send them depending on conditions?

Sure; you're using Express, right? Use res.sendfile :

app.post '/incoming', (req,res) ->
  console.log "Hello, incoming call!"
  message = req.body.Body
  from = req.body.From
  sys.log "From: " + from + ", Message: " + message
  if condition
    res.sendfile 'foo.xml'
  else
    res.sendfile 'bar.xml'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM